识别 2d 数组的行属于 3 个组中的 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)
)
我想计算与预定义组相关的 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 thecount_1
group total - if
2
, then add one to thecount_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三向比较运算符(又名:“太空船运算符”)返回
-1
、0
或1
。您的任务需要将数据识别为三个不同的组。因为您似乎正在评估大于零的整数,并且需要1
、2
和任何其他数字的组,所以您可以将遇到的每个数字与2 进行比较
并利用映射数组来消除对if
块的需要。代码:(演示)
或者类似地,使用
match()
。 演示The three-way comparison operator (aka: "spaceship operator") returns
-1
,0
, or1
. Your task requires identifying data into three distinct groups. Because you are seemingly evaluating on integers greater than zero and need a group for1
,2
, and then any other number, you can compare each encountered number against2
and leverage a mapping array to eliminate the need for anif
block.Code: (Demo)
Or similarly, use
match()
. Demo