0

It feels like a question which has already been asked but I have not been able to find an answer to this.

Say I have a JSON file like this:

    {
        "material": {
            "name": "material",
            "nav": [
              {
                "text": "Backgrounds",
                "url": "backgrounds"
              },
              {
                "text": "Templates",
                "url": "templates"
              }
            ],
            "methods": [{
                "index":[{
                    "title": "Material",
                    "description": "Bla",
                    "keywords": [ "website material", "sozai", "素材" ]
                }],
                "backgrounds":[{
                    "title": "Backgrounds",
                    "description": "Bla.",
                    "keywords": [ "website backgrounds", "tiled backgrounds"]
                }]
            }]
        }
   }

And I json_decode it

$pages = json_decode($data);

To access, say, material > methods > index > title, I could do

$pages->material->methods[0]->index[0]->title

And that is fine (or is it?)

But this would not work with variables mixed in. Elsewhere I get the method name ($method, which will be "index") and put it in place of index.

$pages->material->methods[0]->$method[0]->title

This results in a "Undefined property: stdClass::$i"

I am aware that I can break this problem up into more lines storing the array in a new variable then indexing from there, but that is what I am trying to find the alternative to.

I would like to know if there is a more intuitive way to index this (without the [0]?) without going outside that one expression.

3
  • 1
    $pages->material->methods[0]->{$method}[0]->title? Commented Mar 14, 2015 at 0:04
  • Pass true to the second parameter of json_decode() to force it to be an associative array. Then you can use your variable, eg $pages['material']['methods'][0][$method][0]['title'] Commented Mar 14, 2015 at 0:04
  • @Cyclone and Phil Thank you for your comments, those two ways would both work. I still felt like I was doing something wrong because of the [0]s - but it looks like I'm just thinking too much into it. Commented Mar 14, 2015 at 0:08

1 Answer 1

2

If $method is index, $method[0] is the first character of the string, i.

A simple:

$pages->material->methods[0]->$method->title

or

$pages->material->methods[0]->{$method}->title

(slightly clearer to read, IMO) should suffice.

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

2 Comments

The proposed code does not work since index (or $method) is an array and not an object - but I will use the { } way to separate the variable from the expression.
@tonytony I was going off the statement $method, which will be "index". Adjust as necessary if it's an array.

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.