php数组我有以下数组Array(
[0] => Array
(
[id] => 96
[shipping_no] => 212755-1
[part_no] => reterty [description] => tyrfyt [packaging_type] => PC )
[1] => Array
(
[id] => 96
[shipping_no] => 212755-1
[part_no] => dftgtryh [description] => dfhgfyh [packaging_type] => PC )
[2] => Array
(
[id] => 97
[shipping_no] => 212755-2
[part_no] => ZeoDark
[description] => s%c%s%c%s [packaging_type] => PC ))我该如何对数组进行分组id?是否有任何本机PHP功能可用于执行此操作?如果我foreach上面那么我会得到一个重复的,我怎么能避免呢?在上面的例子中id有2个项目,所以它需要在里面id。编辑:一切工作精细:但有一种方式可以一个人实现相同的目标吗?
3 回答
慕仙森
TA贡献1827条经验 获得超7个赞
没有原生的,只需使用循环。
$result = array();foreach ($data as $element) { $result[$element['id']][] = $element;}
饮歌长啸
TA贡献1951条经验 获得超3个赞
您可以尝试以下方法:
$group = array();foreach ( $array as $value ) { $group[$value['id']][] = $value;}var_dump($group);
输出:
array 96 => array 0 => array 'id' => int 96 'shipping_no' => string '212755-1' (length=8) 'part_no' => string 'reterty' (length=7) 'description' => string 'tyrfyt' (length=6) 'packaging_type' => string 'PC' (length=2) 1 => array 'id' => int 96 'shipping_no' => string '212755-1' (length=8) 'part_no' => string 'dftgtryh' (length=8) 'description' => string 'dfhgfyh' (length=7) 'packaging_type' => string 'PC' (length=2) 97 => array 0 => array 'id' => int 97 'shipping_no' => string '212755-2' (length=8) 'part_no' => string 'ZeoDark' (length=7) 'description' => string 's%c%s%c%s' (length=9) 'packaging_type' => string 'PC' (length=2)
- 3 回答
- 0 关注
- 354 浏览
添加回答
举报
0/150
提交
取消