按列值对关联数组的关联数组中的行进行分组,并保留原始的第一级键
我有一个以下格式的子数组数组:
[
'a' => ['id' => 20, 'name' => 'chimpanzee'],
'b' => ['id' => 40, 'name' => 'meeting'],
'c' => ['id' => 20, 'name' => 'dynasty'],
'd' => ['id' => 50, 'name' => 'chocolate'],
'e' => ['id' => 10, 'name' => 'bananas'],
'f' => ['id' => 50, 'name' => 'fantasy'],
'g' => ['id' => 50, 'name' => 'football']
]
我想根据每个子数组中的 id 字段将其分组为一个新数组。
array
(
10 => array
(
e => array ( id = 10, name = bananas )
)
20 => array
(
a => array ( id = 20, name = chimpanzee )
c => array ( id = 20, name = dynasty )
)
40 => array
(
b => array ( id = 40, name = meeting )
)
50 => array
(
d => array ( id = 50, name = chocolate )
f => array ( id = 50, name = fantasy )
g => array ( id = 50, name = football )
)
)
I have an array of subarrays in the following format:
[
'a' => ['id' => 20, 'name' => 'chimpanzee'],
'b' => ['id' => 40, 'name' => 'meeting'],
'c' => ['id' => 20, 'name' => 'dynasty'],
'd' => ['id' => 50, 'name' => 'chocolate'],
'e' => ['id' => 10, 'name' => 'bananas'],
'f' => ['id' => 50, 'name' => 'fantasy'],
'g' => ['id' => 50, 'name' => 'football']
]
And I would like to group it into a new array based on the id field in each subarray.
array
(
10 => array
(
e => array ( id = 10, name = bananas )
)
20 => array
(
a => array ( id = 20, name = chimpanzee )
c => array ( id = 20, name = dynasty )
)
40 => array
(
b => array ( id = 40, name = meeting )
)
50 => array
(
d => array ( id = 50, name = chocolate )
f => array ( id = 50, name = fantasy )
g => array ( id = 50, name = football )
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
小菜一碟;)
piece of cake ;)
以下代码改编了 @Tim Cooper 的代码,以在内部数组之一不包含 id 的情况下减轻
Undefined index: id
错误:但是,它会下降没有 id 的内部数组。
例如
将完全删除“h”数组。
The following code adapts @Tim Cooper’s code to mitigate
Undefined index: id
errors in the event that one of the inner arrays doesn’t contain an id:However, it will drop inner arrays without an id.
E.g.
will drop the 'h' array completely.
您还可以使用 Arrays::groupBy() 来自 < a href="https://github.com/letsdrink/ouzo-goodies" rel="nofollow noreferrer">ouzo-goodies:
结果:
这里是 数组 和 函数。
You can also use Arrays::groupBy() from ouzo-goodies:
And result:
And here are the docs for Arrays and Functions.
这是一个函数,它将数组作为第一个参数,将条件(字符串或回调函数)作为第二个参数。该函数返回一个新数组,该数组按要求对数组进行分组。
以下是给定的数组:
以及使用带有字符串和回调函数的函数的示例:
两个示例中的结果相同:
在上面的示例中传递回调是多余的,但是当您传递一个对象数组,多维数组,或者有一些你想要分组的任意东西。
Here is a function that will take an array as the first argument and a criteria (a string or callback function) as the second argument. The function returns a new array that groups the array as asked for.
Here is the given array:
And examples using the function with a string and a callback function:
The results are the same in both examples:
Passing the callback is overkill in the example above, but using the callback finds its use when you pass in an array of objects, a multidimensional array, or have some arbitrary thing you want to group by.
也许值得一提的是,您还可以使用 php
array_reduce
函数https://www.php.net/manual/en/function.array-reduce.php
Maybe it's worth to mention that you can also use php
array_reduce
functionhttps://www.php.net/manual/en/function.array-reduce.php
由于 PHP 的排序算法处理多维数组的方式——它按大小排序,然后一次比较一个元素,因此您实际上可以在重组之前对输入使用键保留排序。在函数式编程中,这意味着您不需要将结果数组声明为变量。
代码:(Demo)
我必须说函数式编程对于这项任务来说并不是很有吸引力,因为第一级键必须被保存下来。
虽然 array_walk() 更简洁,但它仍然需要将结果数组作为引用变量传递到闭包中。 (演示)
我可能会推荐一个经典循环来完成此任务。循环唯一需要做的就是重新排列第一级和第二级键。 (演示)
说实话,我预计
ksort()
会更比预循环排序高效,但我想要一个可行的替代方案。Because of how PHP's sorting algorithm treats multidimensional arrays -- it sorts by size, then compares elements one at a time, you can actually use a key-preserving sort on the input BEFORE restructuring. In functional style programming, this means that you don't need to declare the result array as a variable.
Code: (Demo)
I must say that functional programming is not very attractive for this task because the first level keys must be preserved.
Although
array_walk()
is more succinct, it still requires the result array to be passed into the closure as a reference variable. (Demo)I'd probably recommend a classic loop for this task. The only thing the loop needs to do is rearrange the first and second level keys. (Demo)
To be completely honest, I expect that
ksort()
will be more efficient than pre-loop sorting, but I wanted to a viable alternative.