How to get all keys from associative array in php

I have an associative array in php. When I make dying on it, I get the correct values ​​as follows:

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

But when I try to extract the keys of this array into a new array, then I cannot get the keys:

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

Can someone tell me what I'm doing wrong here? I want to get all the keys from my associative array. And for this, I mean the stream: php: how to get an associative array of keys from a numeric index?

+4
source share
3 answers
 $uniqueIds = array_keys($myAssociativeArray[0]); 
+8
source
  <?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); ?> 
+1
source

The next recursively gets all the keys in the associative array

 function getArrayKeysFlat($array) { if(!isset($keys) || !is_array($keys)) { $keys = array(); } foreach($array as $key => $value) { $keys[] = $key; if(is_array($value)) { $keys = array_merge($keys,getArrayKeysFlat($value)); } } return $keys; } 
0
source

All Articles