JSON to PHP array getting "Trying to get property of non-object in" Error

how to get data from json array in php
json to php array online
php parse json array
json to array php
json_decode multidimensional array php
php json foreach key => $value
create json array in php
how to convert json object to array in php

Hello I have Json like this:

[{
    "exam_code": "1",
    "name": "Name1",
    "surname": "Surname1",
    "father_name": "Fname1",
    "id_number": "211111",
    "district_number": "21",
    "school_number": "025",
    "gender": "F",
    "variant": "A",
    "grade": "4",
    "sector": "A",
    "foreign_language": "I",
    "answerList": {
        "gradeFour": {
            "lesson1": ["A", "C", "C", "C", "A", "A", "B", "B", " ", "C", "C", "B", "A", "C", "C", "B", "B", "C", "B", "A"],
            "lesson2": ["B", "A", " ", "C", " ", "B", " ", "B", "B", "C", " ", " ", "B", "A", "A", "A", "C", "A", "B", "B"],
            "lesson3": ["A", "C", "B", "B", "A", "A", "C", "A", "C", "C"],
            "lesson4": ["B", "B", "A", "B", "B"],
            "lesson5": ["B", "A", "A", "B", "B"],
            "lesson6": ["B", "A", "A", "B", "A", "B", "A", "A", "C", "B"]
        }
    }
}]

I am trying to print out lessons answers on foreach loop.

tried below code:

<?php
$msc = microtime(true);


$array = (json_decode($raw_str,true));

foreach($array as $value){
echo $value['id_number'];
    echo '<br/>';

   foreach($value -> answerList->gradeFour as $val){
        echo $val;    
  }

  echo '<br>---------------------------';
  echo '<br>';

}
$msc = microtime(true)-$msc;
echo ($msc * 1000) . ' ms'; // in millseconds
?>

But getting this error

Notice: Trying to get property of non-object in G:\xampp\htdocs\siec\string_test.php on line 50 Notice: Trying to get property of non-object in G:\xampp\htdocs\siec\string_test.php on line 50 Warning: Invalid argument supplied for foreach() in G:\xampp\htdocs\siec\string_test.php on line 50

How do I extract data from JSON with PHP?, Takes a JSON encoded string and converts it into a PHP variable. When TRUE , returned objects will be converted into associative arrays. depth. By following the above steps, you can easily convert any type of PHP array into JSON data format using json_encode() method. If you want to send or exchange small amount of data from one location another location then you can convert the data into JSON format and export it easily.

In your second foreach loop, $value is an array. So you have to use it with brackets [] not -> :

foreach($value['answerList']['gradeFour'] as $val) {

json_decode - Manual, Just paste your JSON to the textarea above and click to the button "Convert" and you will get PHP array in the next textarea. Example of JSON conversion to PHP​  I'm trying to get the value from this following JSON array in a PHP variable. This is a var_dump of the array: array(3) { ["id"]=> string(2) "14" ["css"]=>; string

Your problem in line 5 you must decode it as associative arrays read json_decode

check the code below, it will help you to more understand

 $array = json_decode($raw_str);

 foreach($array as $value){
    echo $value->id_number;
    echo '<br/>';

    foreach($value->answerList->gradeFour as $key => $val){
       echo $key;
       echo "<pre>";
       print_r($val);
       echo "</pre>";

    }

    echo '<br>---------------------------';
    echo '<br>';

}

Convert JSON to PHP Array Online, How to use PHP's json_decode function to convert a JSON string into a PHP variable. How to convert an object to a PHP associative array. $book = json_decode($json); echo $book->title; // Notice: Trying to get property of non-​object echo  First off you have a string. JSON is not an array, an object, or a data structure. JSON is a text-based serialization format - so a fancy string, but still just a string. Decode it in PHP by using json_decode (). $data = json_decode($json); Therein you might find: scalars: strings, ints, floats, and bools.

JSON to PHP Using json_decode, Arrays in PHP will also be converted into JSON when using the PHP function json_encode(): xmlhttp.open("GET", "json_demo_db.php?x=" + dbParam, true); Convert JSON String to PHP Array or Object PHP >= 5.2.0 features a function, json_decode, that decodes a JSON string into a PHP variable. By default it returns an object. The second parameter accepts a boolean that when set as true, tells it to return the objects as associative arrays.

JSON PHP, Although JSON resembles an object or an array, JSON is a string. In order to get one entry, we'll have to access the appropriate array  Converting a PHP object or array into JSON. Just as you use json_decode() to turn JSON into PHP, you can turn PHP into JSON with json_encode().

How to Use JSON Data with PHP or JavaScript – Tania Rascia, <?php // Encode the data. $json = json_encode( array( 1 => array( 'English' => array( 'One', if I try to json_decode this string: "[0-9]{5}", i get this results: <?php PHP File explained: Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.

Comments
  • You start using $value['id_number'] and then switch to object notation access($value -> answerList), you can use one or the other (depending on the second argument to json_decode()) but can't mix them.
  • Thanks for reply... Solved
  • @fischer Great. You can accept the answer, as a guide for next visitors.
  • is it possible to use foreach($value['answerList']['gradeFour'] as $course){ as foreach($value['answerList'][0] as $course){ because gradefour gradefive it dynamic
  • @fischer No, you cant use numbers to access a named array. However, you can iterate on answerList too. like foreach($value['answerList'] as $numberCourses){ and the iterate on $numberCourses to get $course, and so on.
  • This approach adds too much change to the original code: this had to change the way he fetches id_number, and the way he converts from json. Original code obviously wanted array as the result of json_decode, so it is wrong to convert other parts of the code. Also, this answer is simply dumping the whole array of grades and he added pre tag when it is not been asked.