user6464928
user6464928

Reputation:

Getting values from a JSON that has separated string as keys

I have a JSON that I would want to get the values from. However, the key values are typical key. They are not one worded.

[
    {
        "quality-of-service": "Good"
    },
    {
        "quality-of-staff": "Great"
    },
    {
        "quality-of-communication": "Excellent"
    },
    {
        "value-for-money": "Excellent"
    }
]

How do I get the values for the keys.

Upvotes: 0

Views: 22

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

After decoding $array = json_decode($json, true), the first value would be $array[0]['quality-of-service'] which is not a good way to do it. You could loop:

foreach($array as $values) {
    echo key($values) . " is " . current($values);
}

Or you can flatten the array:

$array = array_merge(...$array);

Then use $array['quality-of-service'] or loop it:

foreach($array as $key => $value) {
    echo "$key is $value";
}

Upvotes: 1

Related Questions