0

There are so many JSON/PHP/Decode related post out there... but I am struggling here. I'm pretty sure I have built my JSON incorrectly, but I can't seem to see my mistake. Can anyone help?

{
"badging": [
    {
        "event": [
            {
                "eventName": "Covent Garden",
                "numberOfRooms": "1",
                "mainLang": "xx",
                "timeZone": "saas"
            }
        ]
    },
    {
        "names": [
            {
                "id": "1",
                "fName": "Daniel",
                "pos": "King"
            },
            {
                "id": "2",
                "fName": "Dasha",
                "pos": "Queen"
            }
        ]
    }
]
}

This is posted to a PHP page and json_decode is used

$json = $_POST['feed'];
$list = json_decode($json, TRUE);

And I feel I should be able to access the data like:

echo $list[1][2]['fName'];

or

echo $list->badging[1]->names[2]->fName;

but I can't seem to do it. thanks

3
  • 1
    You can do print_r($list); and see the content of the variable. Commented Mar 5, 2014 at 0:58
  • though my code was still wrong above, this wasn't the thing that was causing me problems. I was taking data from a mySQL db - creating a php string and then using json_encode() - this was then POST to another PHP page where json_decode() was used. every time i used json_encode it would not display. Commented Mar 5, 2014 at 1:43
  • that was only a comment to note you that next time you can do print_r in the whole variable instead of an echo, where if the varianle is an array you won't see the content. Commented Mar 5, 2014 at 1:46

4 Answers 4

1

Try $list['badging'][1]['names'][1]['fName'];
http://codepad.org/IdneHeDL
Also if you omit the true to json_decode then $list->badging[1]->names[1]->fName;
http://codepad.org/WXWkVqo1

Sign up to request clarification or add additional context in comments.

1 Comment

there was actually a problem with the json - your codepad helped me figure it out. I was continually thinking that it was my json or the various types of echoes i tried, it ended up being simple. thanks for the support
1

It should be:

print($list["badging"][1]["names"][0]["fName"]); // outputs "Daniel"
print($list["badging"][1]["names"][1]["fName"]); // outputs "Dasha"

Comments

1

This is how your array looks.

array(1) {
  ["badging"]=>
  array(2) {
    [0]=>
    array(1) {
      ["event"]=>
      array(1) {
        [0]=>
        array(4) {
          ["eventName"]=>
          string(13) "Covent Garden"
          ["numberOfRooms"]=>
          string(1) "1"
          ["mainLang"]=>
          string(2) "xx"
          ["timeZone"]=>
          string(4) "saas"
        }
      }
    }
    [1]=>
    array(1) {
      ["names"]=>
      array(2) {
        [0]=>
        array(3) {
          ["id"]=>
          string(1) "1"
          ["fName"]=>
          string(6) "Daniel"
          ["pos"]=>
          string(4) "King"
        }
        [1]=>
        array(3) {
          ["id"]=>
          string(1) "2"
          ["fName"]=>
          string(5) "Dasha"
          ["pos"]=>
          string(5) "Queen"
        }
      }
    }
  }
}

eg. echo $list['badging'][0]['event'][0]['eventName'];

Comments

0

As per your JSON, by JSON_decode, we get

Array ( [badging] => Array ( [0] => Array ( [event] => Array ( [0] => Array ( [eventName] => Covent Garden [numberOfRooms] => 1 [mainLang] => xx [timeZone] => saas ) ) ) [1] => Array ( [names] => Array ( [0] => Array ( [id] => 1 [fName] => Daniel [pos] => King ) [1] => Array ( [id] => 2 [fName] => Dasha [pos] => Queen ) ) ) ) ) 

So, to access it, you need to it use

echo $list['badging'][1]['names'][0]['fName'];

Happy Coding :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.