AVProgrammer
AVProgrammer

Reputation: 1350

PHP, json_decode, array issue

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?

Upvotes: 1

Views: 1523

Answers (4)

goat
goat

Reputation: 31813

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...

Upvotes: 1

nickb
nickb

Reputation: 59699

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;
}

Upvotes: 3

Brian Driscoll
Brian Driscoll

Reputation: 19635

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'
}

Upvotes: 0

preinheimer
preinheimer

Reputation: 3722

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

Upvotes: 0

Related Questions