识别 2d 数组的行属于 3 个组中的 1 个,并计算每组中的出现次数

发布于 2024-11-06 02:50:36 字数 1817 浏览 9 评论 0原文

我有这个数组:

Array ( 
    [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
    [5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
    [6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
    [7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)     
)

我想计算与预定义组相关的 countThis_id 值的出现次数。

  • 如果 1,则向 count_1 组总计加 1
  • 如果 2,则向 count_2 组总计加 1 ,
  • 如果有任何其他值,则向 the_Rest 组总计添加 1。

我想要的结果:

Array ( 
    [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
    [5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
    [6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
    [7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)   
    [8] => Array ( [count_1] => 2 [count_2] => 2 [the_Rest] => 4)   
)

I have this array:

Array ( 
    [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
    [5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
    [6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
    [7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)     
)

I want to count the occurrences of countThis_id values as they relate to predefined groups.

  • if 1, then add one to the count_1 group total
  • if 2, then add one to the count_2 group total,
  • if any other value, then add one to the the_Rest group total.

My desired result:

Array ( 
    [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
    [5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
    [6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
    [7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)   
    [8] => Array ( [count_1] => 2 [count_2] => 2 [the_Rest] => 4)   
)

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

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

发布评论

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

评论(2

故事还在继续 2024-11-13 02:50:36
$result = array('count_1' => 0, 'count_2'=>0, 'the_Rest'=>0);
foreach($array as $arr){
   if($arr['countThis_id'] == 2){
     $result['count_1']++;
   }
   else if($arr['countThis_id'] == 1){
     $result['count_1']++;
   }
   else {
     $result['the_rest']++;
   }
}

array_push($array, $result);
var_dump($array);
$result = array('count_1' => 0, 'count_2'=>0, 'the_Rest'=>0);
foreach($array as $arr){
   if($arr['countThis_id'] == 2){
     $result['count_1']++;
   }
   else if($arr['countThis_id'] == 1){
     $result['count_1']++;
   }
   else {
     $result['the_rest']++;
   }
}

array_push($array, $result);
var_dump($array);
清晰传感 2024-11-13 02:50:36

三向比较运算符(又名:“太空船运算符”)返回 -101。您的任务需要将数据识别为三个不同的组。因为您似乎正在评估大于零的整数,并且需要 12 和任何其他数字的组,所以您可以将遇到的每个数字与 2 进行比较 并利用映射数组来消除对 if 块的需要。

代码:(演示)

$buckets = [-1 => 'count_1', 0 => 'count_2', 1 => 'the_Rest'];
$totals = array_fill_keys($buckets, 0);
foreach ($array as ['countThis_id' => $id]) {
    ++$totals[$buckets[$id <=> 2]];
}
$array[] = $totals;
var_export($array);

或者类似地,使用 match()演示

$totals = ['count_1' => 0, 'count_2' => 0, 'the_Rest' => 0];
foreach ($array as ['countThis_id' => $id]) {
    ++$totals[match ($id) {
        1 => 'count_1',
        2 => 'count_2',
        default => 'the_Rest',
    }];
}
$array[] = $totals;
var_export($array);

The three-way comparison operator (aka: "spaceship operator") returns -1, 0, or 1. Your task requires identifying data into three distinct groups. Because you are seemingly evaluating on integers greater than zero and need a group for 1, 2, and then any other number, you can compare each encountered number against 2 and leverage a mapping array to eliminate the need for an if block.

Code: (Demo)

$buckets = [-1 => 'count_1', 0 => 'count_2', 1 => 'the_Rest'];
$totals = array_fill_keys($buckets, 0);
foreach ($array as ['countThis_id' => $id]) {
    ++$totals[$buckets[$id <=> 2]];
}
$array[] = $totals;
var_export($array);

Or similarly, use match(). Demo

$totals = ['count_1' => 0, 'count_2' => 0, 'the_Rest' => 0];
foreach ($array as ['countThis_id' => $id]) {
    ++$totals[match ($id) {
        1 => 'count_1',
        2 => 'count_2',
        default => 'the_Rest',
    }];
}
$array[] = $totals;
var_export($array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文