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.