JSON arrays and accessing elements

I am new to PHP but the JSON array is driving me nuts. try as I might I cannot get access to a single element in a multidimensional array.

Feel free to ask a question.

Hi, thanks for replying.
snippet of JSON string:

$string = '[{
“english”:{
“list_countries”:{
“L0”:“choose”,
“L1”:“united kingdom”,
“L2”:“united states”,
“L3”:“germany”,
“L4”:“ukraine”,
“L5”:“russian federation”,
“L6”:“afghanistan”,
“L7”:“albania”,

Tried using json_decode as array and as object.

try as I might I cannot get the value for key “L5” (for example)

Any help would be much appreciated.

I still don’t see a question. I also do not see a proper JSON string.

what is wrong with the JSON string?

It is not correct. You may find this JSON validator tool helpful. https://jsonlint.com/

Yes I have tried that and one other as well. they both say the JSON is correct. (Ignore the “[” at the beginning…)

It would still be wrong. How about posting the actual correct JSON.

$someJSON = ‘{“lang”:{
“lname”:{
“p1”:“Jonathan Suh”,
“p2”:“William Philbin”,
“p3”:“Allison McKinnery”}}}’;

shortened version.

Trying to access “p2” value

<?php
$json = '{
    "lang": {
        "lname": {
            "p1": "Jonathan Suh",
            "p2": "William Philbin",
            "p3": "Allison McKinnery"
        }
    }
}';

$json = json_decode($json, true);
echo $json['lang']['lname']['p2'];

MANY THANKS, that works. Not sure why it was not working before!

Sponsor our Newsletter | Privacy Policy | Terms of Service