搜索多维数组的最佳方法是关键。 喜欢
Array
(
[items] => Array
(
[0] => Array
(
[0] => Array
(
[item1] => Array
(
[item] => a
[url] => xys.com
)
)
)
[1] => Array
(
[1] => Array
(
[item2] => Array
(
[url] => xys.com
[count] => 5
)
)
)
[2] => Array
(
[2] => Array
(
[item3] => Array
(
[url] => abc.com
[count] => 5
)
)
)
[3] => Array
(
[3] => Array
(
[item4] => Array
(
[meta] => b
[count] => 5
)
)
)
[4] => Array
(
[4] => Array
(
[item5] => Array
(
[url] => abc.com
[count] => 5
)
)
)
)
)
我想获取第一次出现或键“URL”的值,值应为“xys.com”
数组可能是随机/动态的,所以我不能使用它的密钥迭代它,我只想让它按第一次出现的密钥进行搜索
谢谢
答案 0 :(得分:1)
我找到了解决方案,这个功能适用于上面的例子:
function searchArrayValueByKey(array $array, $search) {
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {
if ($search === $key)
return $value;
}
return false;
}