I have a multi-dimensional associative array that is encoded into JSON for database storage, and then decoded for display. I am having trouble accessing the resulting array elements.
An example JSON string:
{
"service": "Star Break Repair",
"options": {
"Buy with me -60": "-60.00",
"Bulseye Break Repair": "30.00"
}
}
After decoding this using json_decode($array, true)
(true gets an array, not an object), I get an array as expected:
Array
(
[service] => Star Break Repair
[options] => Array
(
[Buy with me -60] => -60.00
[Bulseye Break Repair] => 30.00
)
)
But when I try and echo a specific element:
echo @key($services['options'][0]);
or
echo $services['options'][0];
I get nothing, blank.
When I try to:
key($services['options'][0])
I get this error:
key() [function.key]: Passed variable is not an array or object in...
I've tried saving the options array as its own PHP variable, and the same thing happens. I can print_r()
either array (the original with the nested options array, or just the options array), but when I try and print a specific element, nothing happens. When I try and print the element key, I get that PHP error.
What's going on?