I need array_keys_recursive ()

$temp = array();
function show_keys($ar)
{
    foreach ($ar as $k => $v )
    {
        $temp[] = $k;
        if (is_array($ar[$k]))
        {
            show_keys ($ar[$k]);
        }
    }

    return $temp;
}

I tried using this function, but it still returns only the first key.

+6
source share
8 answers

Using SPL, switching over keys is pretty simple (save them in another array if you want):

<?php
$arr = array_fill(0,8,range(0,3));
var_dump($arr);
foreach( new RecursiveIteratorIterator(
    new RecursiveArrayIterator($arr),
    RecursiveIteratorIterator::SELF_FIRST)
  as $key => $value){
        var_dump($key);
}
?>
+8
source

I see too many difficult decisions here ...

function array_keys_r($array) {
  $keys = array_keys($array);

  foreach ($array as $i)
    if (is_array($i))
      $keys = array_merge($keys, array_keys_r($i));

  return $keys;
}
+5
source

, show_keys(). .

.

function show_keys($ar)
{
    // Create new temp array inside function so each recursive call gets
    // a separate instance.
    $temp = array();

    foreach ($ar as $k => $v )
    {
        $temp[] = $k;

        // Use $v instead of $ar[$k].
        if (is_array($v))
        {
            // Combine results of recursive show_keys with $temp.
            $temp = array_merge($temp, show_keys($v));
        }
    }

    return $temp;
}
+4

?

$temp , . , .

function show_keys($ar)
{
    $temp = array();
    foreach ($ar as $k => $v )
    {
        $temp[] = $k;
        if (is_array($ar[$k]))
        {
            $temp = array_merge(show_keys ($ar[$k]), $temp);
        }
    }

    return $temp;
}
+2

, . . , ,

function array_keys_flatten_recursive($array, $parentKey = "") {
    $keys = array_keys($array);
    foreach ($array as $parentKey => $i)
      if (is_array($i)) {
        $nestedKeys = array_keys_flatten_recursive($i, $parentKey);
        foreach($nestedKeys as $index => $key) {
            $nestedKeys[$index] = $parentKey . "." . $key;
        }
        $keys = array_merge($keys, $nestedKeys);
      }
    return $keys;
}

:

$array = array(
    "one" => array(
        "one" => "lorem"
    ),
    "two" => array(
        "one" => "lorem"
    )
);


// result : array("one", "two", "one.one", "two.one")
+2

, .

:

$array = [
    'first' => [
        'second' => [
            'third' => 'three',
        ],
        'deuxième' => 'two',
    ],
];

.

$keys = [
    'first',
    'second',
    'third',
    'deuxième',
];

array_keys_recursive, .

$keys = [
    'first' => [
        'second' => [
            'third',
        ],
        'deuxième',
    ],
];

, , :

function array_keys_recursive(array $array) : array
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $index[$key] = array_keys_recursive($value);
        } else {
            $index []= $key;
        }
    }

    return $index ?? [];
}
+1

$temp . , :

global $temp;

.

$temp, , , , $temp, , .

Note that using global variables is not good programming. You need to pass your array as an argument to your recursive calls and modify the passed array by adding the keys found in each iteration, as Alexander and John did.

0
source

Because this function is endless. But my task is to help you)

function show_keys($ar, $temp = array())
{    
    if (!empty($ar)) {
    foreach ($ar as $k => $v )
    {
        $temp[] = $k;
        if (is_array($ar[$k]))
        {
            $temp += show_keys($ar[$k], $temp);
        }
    }
    }

    return $temp;
}
0
source

All Articles