我有一个多维数组搜索
我的数组看起来像这样
Array
(
[0] => Array
(
[id] => 1
[location] => 3
[location_fees] => 3
[gross_percentage] => 25
[transaction_percentage] => 0
[user_name] => admin
[user_id] => 1
[gross] => yes
[transaction] => no
)
[1] => Array
(
[id] => 2
[location] => 5
[location_fees] => 5
[gross_percentage] => 0
[transaction_percentage] => 24
[user_name] => admin
[user_id] => 1
[gross] => no
[transaction] => yes
)
[2] => Array
(
[id] => 3
[location] => 2
[location_fees] => 5
[gross_percentage] => 10
[transaction_percentage] => 0
[user_name] => admin
[user_id] => 1
[gross] => yes
[transaction] => no
)
)
我使用下面的php,我知道它可能可以做得更干净或更少的代码,所以如果你有任何想法如何获得相同的结果,我绝对会学习,我洗耳恭听!
这是我使用的 PHP:
$key = false;
$search = ['gross' => 'yes'];
foreach ($results as $k => $v) {
if ($v['gross'] == $search['gross'] ) {
$key = $k;
$location_fees = array_search('yes', array_column($results, 'gross','location_fees'));
echo "The location fees: ". $location_fees ." % <br><br>";
$gross_percentage = array_search('yes', array_column($results, 'gross', 'gross_percentage'));
echo "The gross_percentage is: ".$gross_percentage ."% <br><br>";
}
}
}
它拒绝显示键 #1 的位置费用
现在奇怪的是,如果我将地点费用更改为 3,它就会显示出来。但它不会带有数字“5”,这也是位置#
这与数字“5”冲突是否有原因?注意键“0”具有位置“3”和位置费用“3”,并且它不会引起任何问题。
我已经在这个问题上坚持了几个小时了,它适用于键#0和#2,没有任何问题。有任何想法吗?
慕婉清6462132