duan010167787 2013-08-10 19:11
浏览 42
已采纳

Foreach循环工作,但输出“无效参数”?

I hope to give enough information here, but I am confused as to why the foreach loop works, it gets each data and outputs it in an li but I am getting an invalid argument error?

Here is the result of the var_dump

array(1) { ["questions"]=> 
    array(2) { ["title"]=> string(5) "Keanu" [1]=> 
        array(1) { ["questionlist"]=> array(2) { [0]=> 
            array(1) { 
                ["a-question"]=> string(1) "1" } [1]=> array(1) { 
                ["a-question"]=> string(5) "civil" } } } } } 

Here is my foreach statement

foreach($questions['questions'] as $key => $value){    
            foreach($value['questionlist'] as $key => $subquestion){ //line 119

                 echo '<li>'.$subquestion['a-question'].'</li>';

            }
}

$questions is a variable used to get the data from the database like this.

$questions = $wpdb->get_row("SELECT * FROM $table_name ORDER BY id DESC LIMIT 1" , ARRAY_A);

The data comes from ajax, I modify the ajax $_POST like this before sending to the database.

    // Add modifications
    $questions['questions'] = $_POST['questions']['data'];

    // DB data
    $name = $wpdb->escape($questions['questions']['title']);
    $data = $wpdb->escape(json_encode($questions));

Screenshot:

enter image description here

I am not sure why I am getting the invalid argument, I suspect its because the array may not be formatted properly, if you need any more information let me know.

A Solution: Thanks to @didierc

I used his code and modified it a bit to display my data in a loop, basically all I did was add another foreach.

 foreach($questions['questions'] as $key => $value){   
   if(is_array($value) && isset($value[ 'questionlist'])){
       foreach($value as $key => $subquestion){ //line 119
           foreach ($subquestion as $key => $value){

               // This loops all the ['a-question'] data
               echo '<li>''.$value['a-question'].''</li>';

           }
        }
     }
  }  
  • 写回答

1条回答 默认 最新

  • dougong9987 2013-08-10 20:17
    关注

    Try this:

    foreach ($questions['questions'] as $key => $value) {   
            if (is_array($value) && isset($value[ 'questionlist']))  {
                foreach ($value['questionlist'] as $subnum => $subquestion) {
                    foreach ($subquestion as $qtitle => $qanswer) {
    

    With variable names hopefully explicit enough. That should get you started.

    NB: The data is probably easier to understand when formatted as below:

    array(1) {
     ["questions"]=> array(2) { 
        ["title"] => string(5) "Keanu"
        [1]      =>  array(1) { 
            ["questionlist"]=> array(2) {
                [0]=>   array(1) { 
                    ["a-question"]=> string(1) "1" 
                }
                [1]=> array(1) { 
                    ["a-question"]=> string(5) "civil"
                }
            } 
        }
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端