我需要解决方法如何比较两个键和值,并从数组中找到最大键值。
解释: - 如果 url 和 con code 两者都高于数量数组应显示和取消匹配数据应该显示请提供相同的溶剂。
$array = array(
array(
"id" => 1,
"amount" => 10,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 2,
"amount" => 3,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 3,
"amount" => 19,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
array(
"id" => 4,
"amount" => 50,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1111' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
);
请检查我想要的结果 在这个示例中,1和2 id具有相同的url和国家/地区代码,因此从这两个我们必须选择更高的数量one.so我们选择这个并且id 3和4不匹配此条件因此它将在此数组中保持相同,因为ID 3有相同的网址,但有不同的国家/地区代码,ID 4有不同的网址。
我想要这个结果。请为此提供溶剂, 提前谢谢...... !!
array( "id" => 1,
"amount" => 10,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia',
"con" => array('US' =>array('id'=> '1','code'=>'US')),
),
array(
"id" => 3,
"amount" => 19,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
array(
"id" => 4,
"amount" => 50,
"url" => 'https://play.google.com/store/apps/details?id=com.my.heroesofutopia1111' ,
"con" => array('US' =>array('id'=> '1','code'=>'US'),'Uk' =>array('id'=> '2','code'=>'UK')),
),
我的代码就是
$output = [];
foreach($array as $data){
if(!isset($output[$data['url']]) ){
$output[$data['url']] = $data;
}
elseif( $output[$data['url']]['amount'] < $data['amount']){
$output[$data['url']] = $data;
}
}
echo "<pre>"; print_r($output);
答案 0 :(得分:0)
这是正确答案
$output = array_reduce($array, function ($carry, $itemA) {
return array_filter($carry, function ($itemB) use($itemA) {
return (
$itemB['url'] != $itemA['url']
|| $itemB['con'] != $itemA['con']
|| $itemB['amount'] >= $itemA['amount']
);
});
}, $array);