2

I have retrieved a PHP variable session that contains a JSON object. The JSON object is the following:

var jsonObject = {
    "menu":{
        "intro":{
            "intosub":"sub_1"
        },
        "vis":{
            "visub":"sub_2"
        }
    }
};

How can I know the length of a part of this JSON object? That is, I want to know how much elements that contain the key Intro and the same with key vis.

I have reviewed some post about this issue, and I found that I have to change the structure of JSON object, but this is not possible. Is there any other way in order to do this?

1
  • 1
    Why is this not possible? You don't necessary need to alter the original json string, so why not just json_decode it and work with it like it's an array? Commented Apr 11, 2016 at 16:14

1 Answer 1

1

First, parse json to php associative array, use json_decode(your_var, true) . You will get a multidimensional array. Then use php count function on desired subarray

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

Comments

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.