How to get all keys out of associative array in php

15,407

Solution 1

$uniqueIds = array_keys($myAssociativeArray[0]);

Solution 2

    <?php
    function multiarray_keys($ar) {

        foreach($ar as $k => $v) {
            $keys[] = $k;
            if (is_array($ar[$k]))
                $keys = array_merge($keys, multiarray_keys($ar[$k]));
        }
        return $keys;
    }
$result = multiarray_keys($myAssociativeArray);
var_dump($result);
    ?> 
Share:
15,407

Related videos on Youtube

Arjit
Author by

Arjit

Updated on September 15, 2022

Comments

  • Arjit
    Arjit over 1 year

    I have an associative array in php. When I am doing a die on it, then I am getting proper values as follows:

    array(1) { [0]=> array(1) { [123]=> string(5) "Hello" }}
    

    But when I am trying extract out keys of this array into an new array, then I am not able to get keys out:

    $uniqueIds = array_keys($myAssociativeArray);
    die(var_dump($uniqueIds));
    int(0) array(1) { [0]=> int(0) } 
    

    Can any one tell me what I am doing wrong here? I want to get all the keys out of my associative array. And for this, I am referring to thread: php: how to get associative array key from numeric index?