计算数组数据
我需要有 4 个这样的关联数组,具有相同的 4 个用户,
Array ( [0] => Array ( [userName] => jim [count] => 6 ) [1] => Array ( [userName] => joe [count] => 6 ) [2] => Array ( [userName] => jesse [count] => 36 ) [3] => Array ( [userName] => mark [count] => 2 ) )
Array ( [0] => Array ( [userName] => jim [count] => 2 ) [1] => Array ( [userName] => jesse [count] => 2 ) [2] => Array ( [userName] => mark [count] => 16 ) )
Array ( [0] => Array ( [userName] => jim [count] => 8 ) )
Array ( )
我们可以说他们被命名为
$values1
$values2
$values3
$values4
他们可能拥有所有 4 个用户,他们也可能没有任何或只有一些
我需要合计所有值并拥有一个像这样的
$people = array(
array("name" => "jim", "score" => 33),
array("name" => "jesse", "score" => 44),
array("name" => "mark", "score" => 66),
array("name" => "joe", "score" => 11)
);
数组一种添加这些值并将这样的一个数组与所有添加的数据相信任的简单方法
我确信我可以通过强力方式找出它,但我想知道是否有一种干净的方法可以做到这一点
I need have 4 associative arrays like this with the same 4 users
Array ( [0] => Array ( [userName] => jim [count] => 6 ) [1] => Array ( [userName] => joe [count] => 6 ) [2] => Array ( [userName] => jesse [count] => 36 ) [3] => Array ( [userName] => mark [count] => 2 ) )
Array ( [0] => Array ( [userName] => jim [count] => 2 ) [1] => Array ( [userName] => jesse [count] => 2 ) [2] => Array ( [userName] => mark [count] => 16 ) )
Array ( [0] => Array ( [userName] => jim [count] => 8 ) )
Array ( )
lets just say they are named
$values1
$values2
$values3
$values4
they may have all 4 users and they may also not have any or just some
I need to total all the values and have one array like this
$people = array(
array("name" => "jim", "score" => 33),
array("name" => "jesse", "score" => 44),
array("name" => "mark", "score" => 66),
array("name" => "joe", "score" => 11)
);
is there an easy way to add these values and contrust one array like this with all the added data
I am sure i can figure it out the brute force way but i was wondering if there is a clean way of doing this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是创建具有此结构的数组:
可以使用 this 循环创建此数组:
从此时起,可以直接使用此结构或将其转换为所需的格式。
The easiest way would be to create an array with this structure:
This array can be created with a this loop:
From this point, either use this structure directly or convert it to the format you want.
你可以这样做:
这会给你这样的结果:
you can do this:
This will give you a result like this:
我认为这就是您希望在 http://www.oriontechnologysolutions.com/stackoverflow 上看到它的实际效果/arraycount.php:
I think this is what you want see it in action at http://www.oriontechnologysolutions.com/stackoverflow/arraycount.php:
这是第三种替代方案,封装为函数格式。 ;-)
演示
Here's a third alternative, encapsulated in to function format. ;-)
DEMO