0

i'm attempting to access an item in a variable being passed to me from a black box. When I use something like

var_dump($var)

to examine the contents of the variable I am given something that looks like this

array(2) {
    'home' => array(6) {
        'label'=> string(4) "home"
        'title'=> string(15) "go to home page"
        'link' => string(45) "....com/store/"
        'first => bool(true)
        'last' => null
        'readonly' => null
    }
    'category2336' => array(6) {
        'label' => string(9) "the title"
        'link' => string(0) ""
        'title' => null
        'first' => null
        'last' => bool(true)
        'readonly' => null
    }
}

How do I access the second key in this array (map)? Specifically the area labeled category2336.

4
  • 2
    Surely it's not too much to expect that one would read the manual before asking such a basic question?
    – Jon
    Commented Dec 11, 2014 at 20:52
  • Simply, $var['category2336'].
    – gen_Eric
    Commented Dec 11, 2014 at 20:55
  • @RocketHazmat nay, I want that key, not the associated array. the category itself will be variable -- it is unknowable and dynamically assigned from my end. Commented Dec 11, 2014 at 20:57
  • @easymoden00b: Will it always be the 2nd element? You can use array_keys($var) to get an array of all the keys, then do $keys[1].
    – gen_Eric
    Commented Dec 11, 2014 at 20:58

1 Answer 1

1

If just want the key, then you can use array_keys to get an array of keys.

$keys = array_keys($var);
echo $keys[1];  // category2336
1
  • Thank you my pony icon'd broheim. Commented Dec 11, 2014 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.