过滤二维数组以仅保留其他重复行中具有最高值的唯一行

发布于 2024-12-15 12:56:01 字数 693 浏览 1 评论 0原文

我有一个如下所示的数组:

$array[1]['first__name'] = 'john'; 
$array[1]['last_name']   = 'anderson'; 
$array[1]['sum']         = 100; 

$array[2]['first__name'] = 'john'; 
$array[2]['last_name']   = 'anderson'; 
$array[2]['sum']         = 200; 

$array[3]['first__name'] = 'julie'; 
$array[3]['last_name']   = 'Scott'; 
$array[3]['sum']         = 300; 

$array[4]['first__name'] = 'Eva'; 
$array[4]['last_name']   = 'Scott'; 
$array[4]['sum']         = 300; 

$array[5]['first__name'] = 'john'; 
$array[5]['last_name']   = 'anderson'; 
$array[5]['sum']         = 300; 

我想比较 sum 的值,其中 first_name = johnlast_name = anderson 并保存最高的值。其余的我想删除。

I have an array that looks like this:

$array[1]['first__name'] = 'john'; 
$array[1]['last_name']   = 'anderson'; 
$array[1]['sum']         = 100; 

$array[2]['first__name'] = 'john'; 
$array[2]['last_name']   = 'anderson'; 
$array[2]['sum']         = 200; 

$array[3]['first__name'] = 'julie'; 
$array[3]['last_name']   = 'Scott'; 
$array[3]['sum']         = 300; 

$array[4]['first__name'] = 'Eva'; 
$array[4]['last_name']   = 'Scott'; 
$array[4]['sum']         = 300; 

$array[5]['first__name'] = 'john'; 
$array[5]['last_name']   = 'anderson'; 
$array[5]['sum']         = 300; 

I want to compare the values of sum where first_name = john, last_name = anderson and save the highest. The rest I would like to remove.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哑剧 2024-12-22 12:56:01
$highest = 0;
foreach ($array as $v){
  if ($v['first_name'] == 'john' && $v['last_name'] == 'anderson'){
    if ($v['sum'] > $highest){$highest = $v['sum'];
  }
}
echo $highest;
$highest = 0;
foreach ($array as $v){
  if ($v['first_name'] == 'john' && $v['last_name'] == 'anderson'){
    if ($v['sum'] > $highest){$highest = $v['sum'];
  }
}
echo $highest;
水晶透心 2024-12-22 12:56:01

这个怎么样?

$scores = array();
foreach( $array as $row ) {
    $name = $row['first_name'] . ' ' . $row['last_name'];
    if( !isset( $scores[$name] ) || ( $row['sum'] > $scores[$name] ) ) {
        $scores[$name] = $row['sum'];
    }
}

How about this?

$scores = array();
foreach( $array as $row ) {
    $name = $row['first_name'] . ' ' . $row['last_name'];
    if( !isset( $scores[$name] ) || ( $row['sum'] > $scores[$name] ) ) {
        $scores[$name] = $row['sum'];
    }
}
陪我终i 2024-12-22 12:56:01

这也将起作用:)

$array[1]['first_name'] = 'john'; 
$array[1]['last_name']   = 'anderson'; 
$array[1]['sum']         = 100; 

$array[2]['first_name'] = 'john'; 
$array[2]['last_name']   = 'anderson'; 
$array[2]['sum']         = 300; 

$array[3]['first_name'] = 'julie'; 
$array[3]['last_name']   = 'Scott'; 
$array[3]['sum']         = 300; 

$array[4]['first_name'] = 'Eva'; 
$array[4]['last_name']   = 'Scott'; 
$array[4]['sum']         = 300; 

$array[5]['first_name'] = 'john'; 
$array[5]['last_name']   = 'anderson'; 
$array[5]['sum']         = 200; 

$cond = array('first_name' => 'john', 'last_name' => 'anderson');

array_walk($array, function($node, $key) use ($cond, &$array) {
    foreach($cond as $k => $v) {
        if(!isset($node[$k]) || $node[$k] != $v) {
            unset($array[$key]);
        }
    }
});
uasort($array, function($a, $b){
    return intval($a['sum']) - intval($b['sum']);
});

$result = end($array);

This will also work :)

$array[1]['first_name'] = 'john'; 
$array[1]['last_name']   = 'anderson'; 
$array[1]['sum']         = 100; 

$array[2]['first_name'] = 'john'; 
$array[2]['last_name']   = 'anderson'; 
$array[2]['sum']         = 300; 

$array[3]['first_name'] = 'julie'; 
$array[3]['last_name']   = 'Scott'; 
$array[3]['sum']         = 300; 

$array[4]['first_name'] = 'Eva'; 
$array[4]['last_name']   = 'Scott'; 
$array[4]['sum']         = 300; 

$array[5]['first_name'] = 'john'; 
$array[5]['last_name']   = 'anderson'; 
$array[5]['sum']         = 200; 

$cond = array('first_name' => 'john', 'last_name' => 'anderson');

array_walk($array, function($node, $key) use ($cond, &$array) {
    foreach($cond as $k => $v) {
        if(!isset($node[$k]) || $node[$k] != $v) {
            unset($array[$key]);
        }
    }
});
uasort($array, function($a, $b){
    return intval($a['sum']) - intval($b['sum']);
});

$result = end($array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文