2015-10-23 2 views
0

Я хочу написать для каждого из них имя и значение объекта, которое находится внутри массива «ques».массив разбора (json) с PHP foreach

мой массив, как

[ 
{ 
    "ques": [ 
     { 
      "name": "comment", 
      "value": "comment me for the reason", 
      "sur_id": "1", 
      "user_id": "[email protected]", 
      "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" 
     } 
    ] 
    } 
] 

Я хочу separte формы значение Ques array.now 'ответ' возвращение нулевой

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); 
} 
+0

'Еогеасп ($ datas-> Ques как $ ключ = > $ values) ' – Thamilan

ответ

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; 

} 
+0

Я попробовал еще вернуть null – user3386779

+0

@ user3386779 попробовал еще раз – Nere

2

Вы используете json_decode($content, true); (consult the manual), чтобы значит, что вы получаете arra y назад, а не объект. Либо удалите true или рассматривать его как ассоциативный массив

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

попробовал оба предложения, предоставленные вами sir.still return null alue – user3386779

+0

Tricky. Есть массив, который обертывает все это. Поэтому, независимо от того, вы должны ссылаться на элемент '0' – Machavity

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 
     ) 

) 
Смежные вопросы