PHP在多维数组中搜索祖父母的值和返回键
我想写一个函数,在多维数组中搜索一个值并返回祖父母的键。请参见下面的阵列层次结构PHP在多维数组中搜索祖父母的值和返回键,php,Php,我想写一个函数,在多维数组中搜索一个值并返回祖父母的键。请参见下面的阵列层次结构 Array ( [results] => Array ( [quote] => Array ( [0] => Array ( [@attributes] => Array
Array
(
[results] => Array
(
[quote] => Array
(
[0] => Array
(
[@attributes] => Array
(
[symbol] => VFORX
)
[LastTradePriceOnly] => 24.79
)
[1] => Array
(
[@attributes] => Array
(
[symbol] => VGSTX
)
[LastTradePriceOnly] => 21.77
)
[2] => Array
(
[@attributes] => Array
(
[symbol] => HPQ
)
[LastTradePriceOnly] => 21.00
)
)
)
)
例如,我想在“symbol”键中搜索值“HPQ”,并返回LastTradePriceOnly值21.00或祖父母的键为[2]
提前感谢您为我入门提供的任何帮助。Hast的答案是解决方案,但要补充一点,您也可以使用foreach语句中的数组键获取“祖父母”。干杯
<?php
$array = array(); // this is your array
$value = 'HPQ';
$result = null;
$grandparent = null;
foreach($array['results']['quote'] as $quote_index => $quote) {
if ($quote['@attributes']['symbol'] == $value) {
$result = $quote['LastTradePriceOnly'];
$grandparent = $quote_index;
}
}
请从您的终端开始编写一些代码,以便我们提供一些建议是的,您最好分享您尝试过的代码。这将有助于社区帮助你。谢谢你,二者兼而有之。这很有效。我将尝试在将来共享我的代码。