欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 二维数组,该如何处理

程序员文章站 2022-05-09 16:30:32
...
php 二维数组
Array ( [0] => Array ( [AdminName] => sysadm [MenuNo] => ) [1] => Array ( [AdminName] => 00000 [MenuNo] => 30,27,32 ) [2] => Array ( [AdminName] => 00000 [MenuNo] => 25,26,27 ) [3] => Array ( [AdminName] => 00001 [MenuNo] => 30,27,32 ) )
数组如上:如何合并AdminName相同的,把MenuNo的值合并到一块
Array ( [0] => Array ( [AdminName] => sysadm [MenuNo] => ) [1] => Array ( [AdminName] => 00000 [MenuNo] => 30,27,32,25,26,27 ) [2] => Array ( [AdminName] => 00001 [MenuNo] => 30,27,32 ) )
------解决方案--------------------
$ar = array (
0 => array ( 'AdminName' => 'sysadm', 'MenuNo' => ''),
1 => array ( 'AdminName' => '00000', 'MenuNo' => '30,27,32' ),
2 => array ( 'AdminName' => '00000', 'MenuNo' => '25,26,27' ),
3 => Array ( 'AdminName' => '00001', 'MenuNo' => '30,27,32' ),
);

$res = array();
foreach($ar as $row) {
if(! isset($res[$row['AdminName']])) $res[$row['AdminName']] = $row;
else $res[$row['AdminName']]['MenuNo'] .= ',' . $row['MenuNo'];
}
print_r(array_values($res));
Array
(
[0] => Array
(
[AdminName] => sysadm
[MenuNo] =>
)

[1] => Array
(
[AdminName] => 00000
[MenuNo] => 30,27,32,25,26,27
)

[2] => Array
(
[AdminName] => 00001
[MenuNo] => 30,27,32
)

)

这在计算方法中称为“聚类”
php 二维数组,该如何处理

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频