1 回答
TA贡献1804条经验 获得超2个赞
您遇到此问题的原因是您致电array_column:
array_column($results, 'gross','location_fees')
这会导致gross值被$results重新索引location_fees,对于您的数据来说,这会导致类似的结果
[3 => 'yes', 5 => 'no', 5 => 'yes']
正如您所看到的,您有两个5无效的数字键,因此第二个数字键会覆盖第一个数字键,最终得到
[3 => 'yes', 5 => 'yes']
而你失败array_search了no,因此你没有得到任何结果。在任何获得复制值的地方都会遇到这个问题。
无论如何,我不确定你为什么要采取这种方法。看来你无论如何都有你想要的数字$v:
$search = ['gross' => 'yes'];
foreach ($results as $k => $v) {
echo "key $k<br>" . PHP_EOL;
if ($v['gross'] == $search['gross'] ) {
$location_fees = $v['location_fees'];
echo "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;
$gross_percentage = $v['gross_percentage'];
echo "The gross_percentage is: ".$gross_percentage ."% <br><br>" . PHP_EOL;
} else {
$tran_perc = $v['transaction_percentage'];
echo "The locations percentage is: ".$tran_perc ." % <br>" . PHP_EOL;
$the_loc = $v['location'];
echo "The location is: ".$the_loc ." <br>" . PHP_EOL;
$location_fees = $v['location_fees'];
echo "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;
}
}
输出:
key 0<br>
The location fees: 3 % <br><br>
The gross_percentage is: 25% <br><br>
key 1<br>
The locations percentage is: 24 % <br>
The location is: 5 <br>
The location fees: 5 % <br><br>
key 2<br>
The location fees: 5 % <br><br>
The gross_percentage is: 10% <br><br>
- 1 回答
- 0 关注
- 252 浏览
添加回答
举报