0

Today I'm trying to get some datas from an PHP array but I don't know how to do this because I'm not supposed to know the name of the first key.

Array
(
[5bnuQsT1Y4S9yk8LRhBeLtT5PWqoAYrv2XlIGu1A] => Array
    (
        [token] => TeamSpeak3_Helper_String Object
            (
                [string:protected] => 5bnuQsT1Y4S9yk8LRhBeLtT5PWqoAYrv2XlIGu1A
                [position:protected] => 0
            )

        [token_type] => 0
        [token_id1] => 8
        [token_id2] => 0
        [token_created] => 1465668613
        [token_description] => 
    )

[df01kyz5BWtgFXDFT+70g5oSze2e3WijYEfbOSDO] => Array
    (
        [token] => TeamSpeak3_Helper_String Object
            (
                [string:protected] => df01kyz5BWtgFXDFT+70g5oSze2e3WijYEfbOSDO
                [position:protected] => 0
            )

        [token_type] => 0
        [token_id1] => 8
        [token_id2] => 0
        [token_created] => 1465668966
        [token_description] => 
    )

 )

I would like to get the value of each key called [string:proected]

I tried to vardump this : $array[0][0] and this : $array[0] but they are set to NULL.

When I do vardump($array["5bnuQsT1Y4S9yk8LRhBeLtT5PWqoAYrv2XlIGu1A"]["token"] I get only the object, but I still need to set a loop to get all the strings.

Can you help me with that?

Thanks!

2
  • foreach loop might help if the structure of the array is same. Commented Jun 11, 2016 at 19:46
  • reset( $array ) php.net/manual/en/function.reset.php, to get the first element of an array, for each string:protected, this should help for each Commented Jun 11, 2016 at 19:51

1 Answer 1

1

You can use a foreach loop to grab each index out of the array like this:

<?php

$data = array (
    '5bnuQsT1Y4S9yk8LRhBeLtT5PWqoAYrv2XlIGu1A' => array (
        'token' => array (
            'string:protected' => '5bnuQsT1Y4S9yk8LRhBeLtT5PWqoAYrv2XlIGu1A',
            'token_type' => 0,
            'token_id1'=> 8,
            'token_id2' => 0,
            'token_created' => 1465668613,
            'token_description' => '',
        ),
    ),
    'df01kyz5BWtgFXDFT+70g5oSze2e3WijYEfbOSDO' => array (
        'token' => array (
            'string:protected' => 'df01kyz5BWtgFXDFT+70g5oSze2e3WijYEfbOSDO',
            'token_type' => 0,
            'token_id1'=> 8,
            'token_id2' => 0,
            'token_created' => 1465668966,
            'token_description' => '',
        ),
    ),
);

foreach($data as $index => $value) {
    echo $data[$index]['token']['string:protected'] . '<br>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Oh thanks you it's work! But can you explain to me why if I write : echo $data[0]['token']['string:protected'] . '<br>'; it does't work ?
because $data[0] dose not exist. its $data[5bnuQsT1Y4S9yk8LRhBeLtT5PWqoAYrv2XlIGu1A], you can do key($array) to get the current key, and reset($array), next(), prev(), end() to get value and more the pointer. Or just use a loop.
Exactly - it's an associative array - associative arrays do not have numerical indices, but only the string index provided. Using this method is smart! :)
Okay I understand now :) Thanks you for your help!
you could do array_values( $array ) and then the number, but you lose the top level keys. And that doesn't explain why you cant get the 0 index, it just makes a new array using only the values of the input 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.