1

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?

1
  • I thought for some reason there was a way to access associative array elements via an index integer. Oh well. Ended up doing a foreach on the array anyways. Thanks for everyone's help. Commented Nov 28, 2011 at 21:30

4 Answers 4

3

This key doesn't exist:

echo $services['options'][0];

Use:

echo $services['options']['Buy with me -60'];
echo $services['options']['Bulseye Break Repair'];

Edit: To print the elements in $services['options'] without knowing their keys, just use a foreach loop:

foreach( $services['options'] as $key => $value)
{
    echo $value;
}
Sign up to request clarification or add additional context in comments.

6 Comments

How do I print those elements without knowing the keys and without a foreach loop? They will always be changing.
@AVProgrammer You can loop through them using a foreach($services['options'] as $key => $value)
@AVProgrammer - I've updated my answer with a loop that prints the values regardless of the key.
var_dump(array_keys($arr['options']));
@AVProgrammer You could do foreach ($services['options'] as $key => value) echo "$key: $value<br />\n"; to print all of them, or echo $services['options'][key($services['options'])]; to print the one at which the array pointer currently resides.
|
1

I would just use foreach like others posted, but this seems kinda what you were trying to do.

echo key($services['options']);
next($services['options']);
echo key($services['options']);

Each array has a hidden position pointer, and those old array iterator functions like key() current() reset() next() etc... use and modify it. Nobody really uses those old array iterator functions anymore since php 4 introduced the foreach construct, which was a long time ago...

Comments

0

The issue is that you have an associative array for your options, not an integer-indexed array.

So, if you want to access the elements of options you need to refer to them by their string keys:

$foo = $services['options']['Buy with me -60'];
$bar = $services['options']['Bulseye Break Repair'];

Now, if you don't know the keys, you can use a foreach loop to iterate over your options array:

foreach($services['options'] as $okey=>$oval) {
    echo $okey; //'Buy with me -60', 'Bulseye Break Repair'
    echo $oval; //'-60.00', '30.00'
}

4 Comments

This is ultimately how I did it. I thought for some reason there was a way to access associative array elements via an index integer. Oh well.
This answer looks eerily similar to an earlier answer.
@nickb Not sure what you're implying here. I got the notification that answers were posted while I was writing my answer, but obviously I didn't get to see them until after I posted mine. So if you're implying that I rehashed an earlier answer I'd strongly suggest that you reconsider your position.
@nickb If you are in fact implying that I cribbed your answer, it's likely worth noting that I posted my answer 2 minutes before you updated yours.
0

Check the way you're accessing elements

<?php

$foo = '{"service":"Star Break Repair","options":{"Buy with me -60":"-60.00","Bulseye Break Repair":"30.00"}}';

$arr = json_decode($foo, true);

var_dump($arr);

echo $arr['options']['Bulseye Break Repair']; //30.00

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.