php 在php中查找多维数组中的所有二级键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1455758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find all second level keys in multi-dimensional array in php
提问by user103219
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.
我想生成使用的第二级密钥的列表。每个记录不包含所有相同的键。但我需要知道所有的钥匙是什么。array_keys() 不起作用,它只返回一个数字列表。
Essentially the output Im looking for is:
本质上,我正在寻找的输出是:
action, id, validate, Base, Ebase, Ftype, Qty, Type, Label, Unit
动作、ID、验证、基础、Ebase、Ftype、数量、类型、标签、单位
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!
谢谢您的帮助!
采纳答案by J.C. Inacio
<?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
编辑:删除了多余的 array_reverse() 函数
回答by ?ystein Riiser Gundersen
array_keys(call_user_func_array('array_merge', $a));
Merge all values and retrieve the resulting keys.
合并所有值并检索结果键。
回答by inkedmn
foreach($bigArray as $array){
foreach($array as $key=>$value){
echo $key;
}
}
That should do what you want.
那应该做你想做的。
回答by Pascal MARTIN
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 implodeto get the string you asked for :
您可以使用implode获取您要求的字符串:
echo implode(', ', $your_keys);
will get you :
会让你:
action, id, base
ie, the list of the keys of the first sub-array.
即,第一个子数组的键列表。
回答by Sy Holloway
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_keys 函数运行 array_map:
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_merge 回调和一个空数组作为初始值对数据运行 array_reduce:
array_reduce(..., 'array_merge', []);
This converts our multiple arrays into 1 flat array:
这将我们的多个数组转换为 1 个平面数组:
[
'key1',
'key1',
'key2',
'key3'
]
Now we strip out our duplicates with array_unique:
现在我们用 array_unique 去除重复项:
array_unique(...)
And end up with all our keys:
最后得到我们所有的钥匙:
[
'key1',
'key2',
'key3'
]
回答by McAngujo
While @raise answers provides a shortcut, it fails with numeric keys. The following should resolve this:
虽然@raise 答案提供了一个快捷方式,但它无法使用数字键。以下应该解决这个问题:
$secondKeys=array_unique(call_user_func_array('array_merge', array_map('array_keys',$a)));
array_map('array_keys',$a): Loop through while getting the keys
array_map('array_keys',$a): 循环获取钥匙
...'array_merge'...: Merge the keys array
...'array_merge'...: 合并键数组
array_unique(...: (optional) Get unique keys.
array_unique(...:(可选)获取唯一键。
I hope it helps someone.
我希望它可以帮助某人。
UPDATE:
更新:
Alternatively you can use
或者你可以使用
$secondKeys=array_unique(array_merge(...array_map('array_keys', $a)));
That provides same answer as above, and much faster.
这提供了与上述相同的答案,而且速度要快得多。
回答by kinderjit singh
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.
试试这个功能。它会如你所愿地返回。
回答by snaphuman
Maybe you can use array_map function, which allows you to avoid array iteration and return an array with the keys you need as values.
也许您可以使用 array_map 函数,它允许您避免数组迭代并返回一个包含您需要的键作为值的数组。
will be like this
会是这样
$newArray = array_map(function($value){return array_keys($value);},$yourArray);
var_dump($newArray);
array (size=2)
0 =>
array (size=9)
0 => string 'action' (length=6)
1 => string 'id' (length=2)
2 => string 'validate' (length=8)
3 => string 'Base' (length=4)
4 => string 'EBase' (length=5)
5 => string 'Qty' (length=3)
6 => string 'Type' (length=4)
7 => string 'Label' (length=5)
8 => string 'Unit' (length=4)
1 =>
array (size=9)
0 => string 'action' (length=6)
1 => string 'id' (length=2)
2 => string 'validate' (length=8)
3 => string 'Base' (length=4)
4 => string 'FType' (length=5)
5 => string 'Qty' (length=3)
6 => string 'Type' (length=4)
7 => string 'Label' (length=5)
8 => string 'Unit' (length=4)
回答by Dirk J. Faber
Onlyif all records have the same keysyou could do:
只有当所有记录都具有相同的键时,您才能执行以下操作:
$firstItem = reset($array);
$keys = array_keys($firstItem);
Obviously, this is not the correct answer to this specific question, where the records have different keys. But this might be the question you find when looking how to retrieve second level keys from an array where all keys are the same (I did). If all the record have the same keys, you can simply use the first item in the array with reset()and get the keys from the first item with array_keys().
显然,这不是这个特定问题的正确答案,其中记录具有不同的键。但这可能是您在查看如何从所有键都相同的数组中检索二级键时发现的问题(我做了)。如果所有记录都具有相同的键,您可以简单地使用数组中的第一项 withreset()并从第一项中获取键array_keys()。
回答by Marek Gralikowski
My proposal, similar to this answerbut faster and using spread operator(PHP 5.6+).
我的建议,类似于这个答案,但速度更快,并且使用扩展运算符(PHP 5.6+)。
array_merge(...array_values($fields))
array_merge(...array_values($fields))
if you want move names to array values and reset keys to 0..n just use array_keysin last step.
如果您想将名称移动到数组值并将键重置为 0..n,只需array_keys在最后一步使用。
array_keys(array_merge(...array_values($fields)))
array_keys(array_merge(...array_values($fields)))

