Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all keys out of associative array in php

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?

like image 809
Arjit Avatar asked Dec 15 '22 13:12

Arjit


2 Answers

$uniqueIds = array_keys($myAssociativeArray[0]);
like image 124
Your Common Sense Avatar answered Dec 26 '22 11:12

Your Common Sense


    <?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);
    ?> 
like image 40
MIIB Avatar answered Dec 26 '22 11:12

MIIB