0
Array
(
    [8] => Array
        (
            [9] => Array
                (
                    [13] => Array()
            )
    )

[14] => Array
    (
        [15] => Array()
    )
)

I want all index value in single array like this one array('8','9','13','14','15');

Zico
  • 2,349
  • 2
  • 22
  • 25

2 Answers2

3

You could combine the array_keys() function with recursion to grab all keys inside your array like shown below:

// Your given array
$array = [
    8 => [
        9 => [
            13 => []
        ]
    ],

    14 => [
        15 => []
    ]
];

// What this function does, is it loops through the array, and if the given
// value is an array too, it does the same until it hits a value which is not an array
function array_keys_recursive($array, $keys = []) {
    foreach ($array as $key => $value) {
        $keys[] = $key;

        if (is_array($value)) {
            $keys = array_merge($keys, array_keys_recursive($value));
        }
    }

    return $keys;
}

// You call it like a so:
$keys = array_keys_recursive($array);
print_r($keys); // Outputs Array ( [0] => 8 [1] => 9 [2] => 13 [3] => 14  [4] => 15 )
Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
0

Here's one possible way to do this. I've use recursion.

<?php


function getkeys($arr) {
    $keys = array();
    foreach (array_keys($arr) as $key) {
        $keys[] = $key;
        if (is_array($arr[$key])) {
            $keys = array_merge($keys, getkeys($arr[$key]));
        }
    }
    return $keys;
}


$arr = array(
    8 => array(
        9 => array(
            13 => array()
        )
    ),
    14 => array(
        15 => array()
    )
);


print_r(getKeys($arr));
coder.in.me
  • 1,048
  • 9
  • 19