0

I want to write for each for separe the object name and value which is inside 'ques' array.

my array as

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

I want to separte value form ques array.now 'answer' 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);
}
1
  • foreach($datas->ques as $key=>$values) Commented Oct 23, 2015 at 13:27

3 Answers 3

2

You're using json_decode($content, true); (consult the manual) so that means you're getting an array back, not an object. Either remove the true or treat it as an associative array

foreach($datas[0]['ques'] as $values)
Sign up to request clarification or add additional context in comments.

2 Comments

tried both suggestion given by you sir.still return null alue
Tricky. There's an array wrapping the whole thing. So regardless you have to reference element 0
1

Modify your code to be like this:

 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;

}

1 Comment

@user3386779 try again
1

Perhaps your real problem is, that you never assign anything to $test[] in the loop?

Anyway - you are mixing object and arrays. As mentioned above, remove the true param from json_decode. The below works for me (hopefully I have understood what the goal is) :

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

then

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

outputs

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
        )

)

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.