2

I want to parse an array with PHP's foreach loop to get the object names and values inside the 'ques' array.I want enter image description here

[
 {
 "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"
}

]
"ques":[
{
    "name": "date",
    "value": "2015-10-23"
    "user_id": "[email protected]",  
},
{
    "name": "select-deopdown",
    "value": ""
},
{
    "name": "true",
    "value": "false"
},
{
    "name": "number",
    "value": "55"
 }
 ]
}
]

I want to separate the name,value and user_id from the 'ques' array:

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

4 Answers 4

3

If you want to make three different array for each value then create three blank array and then storing corresponding values into them in foreach . Am giving you a common example below

$name = array(); 
$values= array(); 
$users = array();   
foreach($datas->ques as $values) {
    $name[] = $values->name;   
    $values[] = $values->value; 
    $users[] = !empty($values->user_id) ? $values->user_id : '';
} 

and further you can modify according your need.

Thank you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for saving my day @Shyam. This foreach loop really helped me without this i may waste the rest of my life figuring out error in my code!
1
This could help

$data = json_decode($json); $result = []; foreach($data as $row) { foreach($row as $k => $v) { $i = 0; foreach($v as $key => $val) { foreach($val as $k1 => $v1) { $result[$i][$k1] = $v1; }
$i++; } } }

echo json_encode($result);

1 Comment

$json contains your data that you have shown. $results contains many rows in it . in each row keys are name, value and user_id with its respective values. for you to understand the structure of $result, i have included the echo json_encode($result); try running the entire code
1
foreach($datas->ques as $values)
 {
     $name = $values['name'];
     $value = $values['value'];
     $user_id = $values['user_id'];
 }

Comments

0

I solved this by following code.Thanks for 3 of them to guide by giving ideas.

$datas = json_decode($content, true);
foreach($datas as $values)
 {
 $name = $values['name'];
 $value = $values['value'];
 $user_id = $values['user_id'];
 $test[]= array('user'=>$user,'name'=>$name,'value'=>$value);
 } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.