使用PHP foreach

时间:2015-10-23 13:26:33

标签: php arrays json

我想为每个人写一些对象名称和内部的值'问题'阵列。

我的数组为

 [
 {
    "ques": [
        {
            "name": "comment",
            "value": "comment me for the reason",
            "sur_id": "1",
            "user_id": "admin@gmail.com",
            "pagename": "question_response"
        },
        {
            "name": "check-box[]",
            "value": "1"
        },
        {
            "name": "radio",
            "value": "radio 2"
        },
        {
            "name": "yes",
            "value": "no"
        },
        {
            "name": "date",
            "value": "2015-10-23"
        },
        {
            "name": "select-deopdown",
            "value": ""
        },
        {
            "name": "true",
            "value": "false"
        },
        {
            "name": "number",
            "value": "55"
        }
    ]
  }
]

我想从问题数组中选择价值形式。现在回答' return null

  while ($fetch = mysql_fetch_array($query1)) {
   $content = $fetch['CONTENT_VALUES'];
  // print_r($content);
  $content_value= mb_convert_encoding($content ,"UTF-8");
    $datas = json_decode($content, true);
     foreach($datas->ques as $values)
     {
         echo $values->value . "\n";
          print_r($values);
     }
   $test[] = array('ques' =>  $datas ,'answer'=>$values);
}

3 个答案:

答案 0 :(得分:2)

您正在使用json_decode($content, true);consult the manual),这意味着您将获得一个数组,而不是一个对象。删除true或将其视为关联数组

foreach($datas[0]['ques'] as $values)

答案 1 :(得分:1)

将您的代码修改为:

 while ($fetch = mysql_fetch_array($query1)) {
 $content = $fetch['CONTENT_VALUES'];
  $content_value= mb_convert_encoding($content ,"UTF-8");
    $datas = json_decode($content); //To return as Object
    foreach($array[0]->ques as $values):
        echo $values->name.'<br/>'; //For example
    endforeach;

}

答案 2 :(得分:1)

也许您的真正的问题是,您从未在循环中为$test[]分配任何内容?

无论如何 - 你正在混合对象和数组。如上所述,从true中删除json_decode参数。以下内容对我有用(希望我已经理解了目标是什么):

//test load of the JSON
$content = file_get_contents('test.json');

$test = array();
$datas = json_decode($content);
foreach($datas[0]->ques as $item) {
    $test[] = array('ques' => $item->name, 'answer' => $item->value);
}

然后

echo '<pre>';
print_r($test);
echo '<pre>';

输出

Array
(
    [0] => Array
        (
            [ques] => comment
            [answer] => comment me for the reason
        )

    [1] => Array
        (
            [ques] => check-box[]
            [answer] => 1
        )

    [2] => Array
        (
            [ques] => radio
            [answer] => radio 2
        )

    [3] => Array
        (
            [ques] => yes
            [answer] => no
        )

    [4] => Array
        (
            [ques] => date
            [answer] => 2015-10-23
        )

    [5] => Array
        (
            [ques] => select-deopdown
            [answer] => 
        )

    [6] => Array
        (
            [ques] => true
            [answer] => false
        )

    [7] => Array
        (
            [ques] => number
            [answer] => 55
        )

)