0

I printed out what was supposed to be a multidimensional array in php but saw this instead. It was throwing errors when I tried to access $array['order_item']['sku']. How do I convert this to a proper multidimensional array?

Array
(
[order_item] => [{"name":"Product","sku":"14b6c7e2f838fd356","description":"Product Standard Download","price":"76.0000","qty":1,"tax":0}]
[customer] => {"first_name":"Johny","last_name":"Smith","email":"[email protected]"}
)
2
  • Loop over the array and call json_decode on each value. Commented Oct 8, 2015 at 17:38
  • Also, the order_item key would be an array of arrays. So the first sku would be $array['order_item'][0]['sku']. Appears that this structure would allow multiple "order items" per "customer" Commented Oct 8, 2015 at 17:41

2 Answers 2

1

try this one

$array = array(
    'order_item' => '[{"name":"Product","sku":"14b6c7e2f838fd356","description":"Product Standard Download","price":"76.0000","qty":1,"tax":0}]',
    'customer' => '{"first_name":"Johny","last_name":"Smith","email":"[email protected]"}'
);

foreach ($array as $key => $value) {
    $array[$key] = json_decode($array[$key], TRUE);
}
echo $array['order_item'][0]['sku'];

this will give: 14b6c7e2f838fd356

Sign up to request clarification or add additional context in comments.

Comments

0

The proper way to create multidimensional arrays in php is as follows:

$array['order_item']['name'] = "Product";

And so on

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.