-1

I'm attempting to decode the following output to a PHP array and I'm running into some issues. Google hasn't been much help.

I receive the string as JSON from an existing application:

[{"1":{"product":"ABC47","quantity":"2"},"2":{"product":"ABC30","quantity":"2"}}]

What I have tried is:

$arr = json_decode($json,true);

foreach ($arr as $key => $value){
echo " Key: $key; Value $value<br />\n" ;
}

The issue is that it's adding strange objects, as seen through var_dump:

array(1) { 
    [0]=> array(2) { 
    [1]=> array(2) { ["product"]=> string(5) "ABC47" ["quantity"]=> string(1) "2" }
    [2]=> array(2) { ["product"]=> string(5) "ABC30" ["quantity"]=> string(1) "2" } 
    }
  }

Followed by an error message on my foreach:

Notice: Array to string conversion in pm.php on line 20

What I'm trying to accomplish is getting the product value and the quantity value matched up, so looping the PHP array gives me a neat list like

ABC47 2

ABC30 2

That I can put into a table, database etc. So I need a way for only the product and quantity to be passed into a new array and neatly present it to either the front end and/or into another block of code for database processing. I can't quite figure out this nested array thing.

Thank you.

2
  • It's an array inside an array.. so 1 foreach won't do it.. Commented Nov 20, 2014 at 9:58
  • JSon is correct, nothing is strange. Note that you have multidimensinal array you have to use nested foreach because on the first foreach you will get an array Commented Nov 20, 2014 at 9:59

3 Answers 3

1

$value within your foreach loop refers to an array, which you cannot echo.

As far as I can tell, this is what you are looking for:

$arr = json_decode($json, true)[0];
foreach ($arr as $key => $value){
    echo " Key: $value[product]; Value: $value[quantity]<br />\n" ;
}

Demo

2
  • Perfect. This also gives me more insight on how to handle this. Not very familiar with JSON or arrays, but necessity has me solve this. Today I learned! Commented Nov 20, 2014 at 10:11
  • I'm glad to have helped (and hopefully taught you something). :) Commented Nov 20, 2014 at 10:12
1
$jsonString = '[{"1":{"product":"ABC47","quantity":"2"},"2":{"product":"ABC30","quantity":"2"}}]';

$json = json_decode($jsonString, true);

foreach($json[0] as $key => $productArray) {
    echo $productArray['product'] . ' - ' . $productArray['quantity'] . PHP_EOL;
}
0

Looks like you don't understand nesting levels as well as you should.

Try this :

$arr = json_decode($json,true);
foreach ($arr as $list) {
    foreach ($list as $item) {
        echo $item['product'];
    }
}
1
  • 1
    No need for being rude. "As you should" would imply I'm a professional programmer, which I'm not. This particular data is being passed by code not generated by me, by software from a finance department. No programmers work here and they wanted to know if they could export particular sets of data for use outside of the software. Commented Nov 20, 2014 at 10:07

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.