我有一个关于多dim数组的简单问题,我想删除任何redundent元素让我说在我的情况下,[serviceMethod] => PL
来了2次,我想搜索'PL'关于{{ 1}}如果一个元素价格较低,我想保留它并删除数组中的另一个
[APIPriceTax]
)
这是我的伪代码:Array (
[0] => Array (
[carrierIDs] => 150
[serviceMethod] => CP
[APIPriceTax] => 30.63
[APIPriceWithOutTax] 28.32
[APIServiceName] => Xpresspost USA
[APIExpectedTransitDay] => 2
)
[1] => Array (
[carrierIDs] => 155
[serviceMethod] => PL
[APIPriceTax] => 84.13
[APIPriceWithOutTax] => 73.8
[APIServiceName] => PurolatorExpressU.S.
[APIExpectedTransitDay] => 1
)
[2] => Array (
[carrierIDs] => 164
[serviceMethod] => PL
[APIPriceTax] => 25.48
[APIPriceWithOutTax] => 22.35
[APIServiceName] => PurolatorGroundU.S.
[APIExpectedTransitDay] => 3
)
是实际数组
$carrierAddedToList
答案 0 :(得分:1)
由于没有in_array()
搜索多维元素,因此需要构建一个由serviceMethod键入的关联数组。然后,您可以使用isset()
检查我们是否已经拥有该方法的元素。
$newArray = array();
foreach ($carrierAddedToList as $cK) {
$sm = $cK['serviceMethod'];
if (!isset($newArray[$sm]) || $newArray[$sm]['APIPriceTax'] > $cK['ApiPriceTax']) {
$newArray[$sm] = $cK;
}
}
// Now convert associative array to indexed:
$newArray = array_values($newArray);