As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:

$result = json_decode($jsondata, true);

If you want integer keys instead of whatever the property names are:

$result = array_values(json_decode($jsondata, true));

However, with your current decode you just access it as an object:

print_r($obj->Result);
Answer from Stephen on Stack Overflow
🌐
PHP
php.net › manual › en › function.json-decode.php
PHP: json_decode - Manual
When null, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY is set in the flags. ... Maximum nesting depth of the structure being decoded.
Discussions

php - How to json_decode an array of objects? - Stack Overflow
When we json_encode an array of objects, how to get back the same array of objects when you json_decode the encoded data? ... Save this answer. ... Show activity on this post. I tried following code on PHP 5.6.11-1ubuntu3.1 (cli), and surprisingly got exactly what I wanted. More on stackoverflow.com
🌐 stackoverflow.com
March 12, 2013
decode json array with multiple objects in php - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Closed 10 years ago. I am unable to decode JSON array with multiple objects. More on stackoverflow.com
🌐 stackoverflow.com
php json decode object with array of multiple objects - Stack Overflow
You can decode these values from json to object. Copy$result = json_decode('{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}'); ... Save this answer. ... Show activity on this post. You can access each "value" by accessing regular php array ... More on stackoverflow.com
🌐 stackoverflow.com
December 15, 2015
How can I get JSON objects by array?
Json_decode() returns an object by default but you can provide the correct 2nd parameter to get an indexed array instead. Look it up in the php manual on line (at PHP.net) for specifics. More on reddit.com
🌐 r/PHPhelp
6
1
March 28, 2024
🌐
ReqBin
reqbin.com › code › php › 0uwqq41y › json-to-array-php-example
How do I convert JSON to PHP array?
In PHP, such an object can be created using a multidimensional array. The following is an example of nesting a JSON string into a PHP array using the json_decode() function:
🌐
PHPpot
phppot.com › php › php-object-to-array
PHP Object to Array Convert using JSON Decode - PHPpot
This quick example performs a PHP object to array conversion in a single step. It creates an object bundle and sets the properties. It uses JSON encode() decode() function for the conversion.
Top answer
1 of 4
3

Give this a try:

$json = '
{
   "article_details":[  
      {  
         "article_code ":"000000000300028156 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028174 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028126 ",
         "diff_amnt ":"1 "
      }
   ],
   "article":[  
      {  
         "article_code ":"000000000300028156 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028174 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028126 ",
         "diff_amnt ":"1 "
      }
   ]
}';

$key = 'article_code '; // Since your key is 'article_code ' but not 'article_code', I have to do this
$dataObject = json_decode($json);
echo $dataObject->article_details[0]->$key; // This will out put: 000000000300028156 
$dataArray = json_decode($json, true);
echo $dataArray['article_details'][0]['article_code ']; // This will out put: 000000000300028156 
2 of 4
2

Use json_decode() to decode a JSON string, like this:

$json_array = json_decode($json, true);
                                  ^ When TRUE, returned objects will be converted into associative arrays.

So your code should be like this:

// Here $json is your json string
$json_array = json_decode($json, true);

foreach($json_array as arrays){
    echo $key . "<br />";
    foreach($arrays as $array){
        foreach($array as value){
            echo value . "<br />";
        }
    }
    echo "<br />";
}

Output:

article_details
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1

article
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1 
Find elsewhere
🌐
PHP: json_decode()
parthpatel.net › home › php › php: json_decode() | how to decode json to array in php
PHP: json_decode() | How to decode json to array in PHP | Parth Patel - a Web Developer
September 9, 2021 - flags: It includes bitmask of JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING,, JSON_THROW_ON_ERROR and other JSON constants. ... The json_decode() function decodes the JSON string to appropriate PHP type based on the parameter.
🌐
W3Schools
w3schools.com › php › func_json_decode.asp
PHP json_decode() Function
The json_decode() function is used to decode or convert a JSON object to a PHP object. ... $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); echo $obj->Peter; echo $obj->Ben; echo $obj->Joe; Run Example » · How to ...
🌐
Reddit
reddit.com › r/phphelp › how can i get json objects by array?
r/PHPhelp on Reddit: How can I get JSON objects by array?
March 28, 2024 -

Hello, I’m not certain how to word this question and it not be confusing so I’ll just provide the use case in hopes someone can lend a solution.

I have a json file and each object contains an ID element that is a number.

In the database, I could have something like 2,17,25.

How would I go about selecting those items into an associated php array?

So currently I’m using a foreach loop to output the entire thing, and I suppose I could use in_array() inside of the loop, but that seems inefficient as it is a large json with a few hundred entries and that would mean a few hundred in_array calls. Perhaps some kind of array diff call before the foreach loop maybe? But I’m not sure how to implement that when the json array is key value and I have only the comma delimited string. Any help appreciated.

🌐
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-json-parsing.php
How to Encode and Decode JSON Data in PHP - Tutorial Republic
The output of the above example will look something like this: object(stdClass)#1 (4) { ["Peter"]=> int(65) ["Harry"]=> int(80) ["John"]=> int(78) ["Clark"]=> int(90) } By default the json_decode() function returns an object.
🌐
ReqBin
reqbin.com › code › php › zg4d50d8 › php-json-decode-example
How do I decode JSON in PHP?
July 7, 2023 - To decode a JSON string or file in PHP, you can use the json_decode($json, $assoc, $depth, $options) function. The first parameter specifies the string to be decoded. The second optional parameter sets whether the returned object should be converted ...
🌐
Coddy.Tech
coddy.tech › docs › php › json_decode()
PHP json_decode(): Parse JSON into Arrays or Objects | Coddy
3 weeks ago - json_decode($json, true) turns a JSON string into a PHP associative array; without true you get stdClass objects.
🌐
CodeSignal
codesignal.com › learn › courses › handling-json-files-with-php › lessons › parsing-json-arrays-with-php
Parsing JSON Arrays with PHP
In this lesson, we'll build on what you've previously learned to explore how to extract data from JSON arrays using PHP's built-in functions. A JSON array is a collection of ordered items enclosed in square brackets, [ ]. Each item in an array could be an object, a string, a number, or even another array.
🌐
Jobtensor
jobtensor.com › Tutorial › PHP › en › JSON
PHP JSON - json_encode(), json_decode() | jobtensor
The json_decode() function is used to decode a JSON object into a PHP associative array or object. It has an optional second parameter that gives a PHP associative array instead of the default PHP object.
🌐
W3Schools
w3schools.com › php › php_json.asp
PHP and JSON
The function has a second parameter, and when set to true, JSON objects are decoded into associative arrays. This example decodes JSON data into a PHP associative array: <?php $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj, true)); ?> Run Example » · Here are two examples of how to access the decoded values from an object and from an associative array: This example shows how to access the values from a PHP object: <?php $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); echo $obj->Peter; echo $obj->Ben; echo $obj->Joe; ?> Run Example » ·