Find all second level keys in multi-dimensional ar

2019-02-04 06:51发布

I want to generate a list of the second level of keys used. Each record does not contain all of the same keys. But I need to know what all of the keys are. array_keys() doesn't work, it only returns a list of numbers.

Essentially the output Im looking for is:

action, id, validate, Base, Ebase, Ftype, Qty, Type, Label, Unit

I have a large multi-dimensional array that follows the format:

Array
(
    [0] => Array
        (
            [action] => A
            [id] => 1
            [validate] => yes
            [Base] => Array
                (
                    [id] => 2945
                )

            [EBase] => Array
                (
                    [id] => 398
                )

            [Qty] => 1
            [Type] => Array
                (
                    [id] => 12027
                )

            [Label] => asfhjaflksdkfhalsdfasdfasdf
            [Unit] => asdfas
        )

    [1] => Array
        (
            [action] => A
            [id] => 2
            [validate] => yes
            [Base] => Array
                (
                    [id] => 1986
                )

            [FType] => Array
                (
                    [id] => 6
                )

            [Qty] => 1
            [Type] => Array
                (
                    [id] => 13835
                )

            [Label] => asdssdasasdf
            [Unit] => asdger
        )
)

Thanks for the help!

9条回答
萌系小妹纸
2楼-- · 2019-02-04 07:12
<?php

// Gets a list of all the 2nd-level keys in the array
function getL2Keys($array)
{
    $result = array();
    foreach($array as $sub) {
        $result = array_merge($result, $sub);
    }        
    return array_keys($result);
}

?>

edit: removed superfluous array_reverse() function

查看更多
狗以群分
3楼-- · 2019-02-04 07:14
function  __getAll2Keys($array_val){
        $result = array();
        $firstKeys = array_keys($array_val);
        for($i=0;$i<count($firstKeys);$i++){
            $key = $firstKeys[$i];
            $result = array_merge($result,array_keys($array_val[$key]));
        }
        return $result;
    }

try this function. It will return as you want.

查看更多
Root(大扎)
4楼-- · 2019-02-04 07:20

With this function you can get all keys from a multidimensional array

function arrayKeys($array, &$keys = array()) {        
        foreach ($array as $key => $value) {
            $keys[] = $key;
            if (is_array($value)) {                
                $this->arrayKeys($value, $keys);
            }
        }
        return $keys;
}
查看更多
Lonely孤独者°
5楼-- · 2019-02-04 07:21

What about something like this :

$your_keys = array_keys($your_array[0]);

Of course, this is considering all sub-arrays have the same keys ; in this case, you only need the keys of the first sub-array (no need to iterate over all first-level sub-arrays, I guess)


And, as a shortened / simplified example :

$your_array = array(
    array(
        'action' => 'A',
        'id' => 1,
        'base' => array('id' => 145),
    ),
    array(
        'action' => 'B',
        'id' => 2,
        'base' => array('id' => 145),
    ),
    array(
        'action' => 'C',
        'id' => 3,
        'base' => array('id' => 145),
    )
);

$your_keys = array_keys($your_array[0]);
var_dump($your_keys);

Will get you :

array
  0 => string 'action' (length=6)
  1 => string 'id' (length=2)
  2 => string 'base' (length=4)

You can the use implode to get the string you asked for :

echo implode(', ', $your_keys);

will get you :

action, id, base

ie, the list of the keys of the first sub-array.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-02-04 07:21

One liner:

$keys=array_unique(array_reduce(array_map('array_keys',$data),'array_merge',[]));

Or in a function:

function get_array_children_keys($data) {
    return array_unique(
        array_reduce(array_map('array_keys', $data), 'array_merge', [])
    );
}

Now lets break this down with an example, here is some sample data:

[
    ['key1' => 0],
    ['key1' => 0, 'key2' => 0],
    ['key3' => 0]
]

Starting with the inner most function, we run array_map with the array_keys function:

array_map('array_keys', $data)

This gives us the keys of from all child arrays

[
    ['key1'],
    ['key1', 'key2'],
    ['key3']
]

Then we run the array_reduce on the data with the array_merge callback and an empty array as the initial value:

array_reduce(..., 'array_merge', []);

This converts our multiple arrays into 1 flat array:

[
    'key1',
    'key1',
    'key2',
    'key3'
]

Now we strip out our duplicates with array_unique:

array_unique(...)

And end up with all our keys:

[
    'key1',
    'key2',
    'key3'
]
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-02-04 07:22
array_keys(call_user_func_array('array_merge', $a));

Merge all values and retrieve the resulting keys.

查看更多
登录 后发表回答