搜索在多维数组键和返回路径,它 [英] Searching for key in multidimensional array and returning path to it
问题描述
我需要找到数组中的特定键,返回无论其价值和路径查找键即可。例如:
$阵列=阵列(
FS1'=>阵列(
'ID1'=> 0,
'富'=> 1,
FS2'=>阵列(
ID2'=> 1,
'foo2的'=> 2,
FS3'=>阵列(
ID3'=>空值,
)
FS4'=>阵列(
ID4=> 4,
'酒吧'=> 1,
)
)
)
);搜索($阵列,'FS3'); //返回('fs1.fs2.fs3',阵列('ID3'=>空))
搜索($阵列,'FS2'); //返回('fs1.fs2',阵列(ID2'=> 1,...))
我已经能够通过数组递归找到正确的钥匙,并使用 RecursiveArrayIterator
(如下图所示)返回数据,但我不知道最好的方法来跟踪我目前什么路径。
$ I =新RecursiveIteratorIterator
新RecursiveArrayIterator($数组)
RecursiveIteratorIterator :: SELF_FIRST);
的foreach($ I $作为重点=>的值){
如果($关键=== $搜索){
返回$价值;
}
}
刚刚完成的缘故和未来的游客。结合例子code以上,我评论了关于领取钥匙的答案。这是一个工作函数,将一个小的变化返回请求的结果。在我返回数组我返回键路径
和值
,而不是要求 0
和 $搜索
的钥匙。我觉得这是更详细的,更容易处理。
< PHP
$阵列=阵列(
FS1'=>阵列(
'ID1'=> 0,
'富'=> 1,
FS2'=>阵列(
ID2'=> 1,
'foo2的'=> 2,
FS3'=>阵列(
ID3'=>空值,
)
FS4'=>阵列(
ID4=> 4,
'酒吧'=> 1,
)
)
)
);功能搜索($数组$ searchKey =''){
//阵列上创建一个递归迭代循环递归
$ ITER =新RecursiveIteratorIterator(
新RecursiveArrayIterator($数组)
RecursiveIteratorIterator :: SELF_FIRST); //循环迭代
的foreach(ITER $ $作为重点=> $值){
//如果该键我们的搜索匹配
如果($关键=== $ searchKey){
//将当前关键
$键=阵列($键);
//循环向上递归链
为($ I = $ iter-> getDepth() - 1; $ I> = 0; $ I - ){
//添加每个父键
array_unshift($键,$ iter-> getSubIterator($ⅰ) - >键());
}
//返回我们的输出数组
返回数组('路径'=>'。'破灭(,$键),价值= GT; $值);
}
}
//返回false,如果未找到
返回false;
}$ searchResult1 =搜索($数组,'FS2');
$ searchResult2 =搜索($数组,'FS3');
回声< pre>中;
的print_r($ searchResult1);
的print_r($ searchResult2);
输出:
阵列
(
[路径] => fs1.fs2
[值] =>排列
(
[ID2] => 1
[foo2的] => 2
[FS3] =>排列
(
[ID3] =>
) [FS4] =>排列
(
[ID4] => 4
[巴] => 1
) ))
排列
(
[路径] => fs1.fs2.fs3
[值] =>排列
(
[ID3] =>
))
I need to find a specific key in an array, and return both its value and the path to find that key. Example:
$array = array(
'fs1' => array(
'id1' => 0,
'foo' => 1,
'fs2' => array(
'id2' => 1,
'foo2' => 2,
'fs3' => array(
'id3' => null,
),
'fs4' => array(
'id4' => 4,
'bar' => 1,
),
),
),
);
search($array, 'fs3'); // Returns ('fs1.fs2.fs3', array('id3' => null))
search($array, 'fs2'); // Returns ('fs1.fs2', array('id2' => 1, ... ))
I've been able to recurse through the array to find the correct key and return the data using RecursiveArrayIterator
(shown below), but I don't know the best way to keep track of what path I'm currently on.
$i = new RecursiveIteratorIterator
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($i as $key => value) {
if ($key === $search) {
return $value;
}
}
Just for completion sake and future visitors. Combining the example code above and the answer I commented about to get the keys. Here is a working function that will return the requested results with one small change. In my return array I return the keys path
and value
instead of the requested 0
and $search
for the keys. I find this more verbose and easier to handle.
<?php
$array = array(
'fs1' => array(
'id1' => 0,
'foo' => 1,
'fs2' => array(
'id2' => 1,
'foo2' => 2,
'fs3' => array(
'id3' => null,
),
'fs4' => array(
'id4' => 4,
'bar' => 1,
),
),
),
);
function search($array, $searchKey=''){
//create a recursive iterator to loop over the array recursively
$iter = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST);
//loop over the iterator
foreach ($iter as $key => $value) {
//if the key matches our search
if ($key === $searchKey) {
//add the current key
$keys = array($key);
//loop up the recursive chain
for($i=$iter->getDepth()-1;$i>=0;$i--){
//add each parent key
array_unshift($keys, $iter->getSubIterator($i)->key());
}
//return our output array
return array('path'=>implode('.', $keys), 'value'=>$value);
}
}
//return false if not found
return false;
}
$searchResult1 = search($array, 'fs2');
$searchResult2 = search($array, 'fs3');
echo "<pre>";
print_r($searchResult1);
print_r($searchResult2);
outputs:
Array
(
[path] => fs1.fs2
[value] => Array
(
[id2] => 1
[foo2] => 2
[fs3] => Array
(
[id3] =>
)
[fs4] => Array
(
[id4] => 4
[bar] => 1
)
)
)
Array
(
[path] => fs1.fs2.fs3
[value] => Array
(
[id3] =>
)
)
这篇关于搜索在多维数组键和返回路径,它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!