Home > Backend Development > PHP Tutorial > php 二维数组,该如何处理

php 二维数组,该如何处理

WBOY
Release: 2016-06-13 12:01:19
Original
941 people have browsed it

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 (<br />  0 => array ( 'AdminName' => 'sysadm', 'MenuNo' => ''),<br />  1 => array ( 'AdminName' => '00000',  'MenuNo' => '30,27,32' ),<br />  2 => array ( 'AdminName' => '00000',  'MenuNo' => '25,26,27' ),<br />  3 => Array ( 'AdminName' => '00001',  'MenuNo' => '30,27,32' ),<br />);<br /><br />$res = array();<br />foreach($ar as $row) {<br />  if(! isset($res[$row['AdminName']])) $res[$row['AdminName']] = $row;<br />  else $res[$row['AdminName']]['MenuNo'] .= ',' . $row['MenuNo'];<br />}<br />print_r(array_values($res));
Copy after login
Array<br />(<br />    [0] => Array<br />        (<br />            [AdminName] => sysadm<br />            [MenuNo] => <br />        )<br /><br />    [1] => Array<br />        (<br />            [AdminName] => 00000<br />            [MenuNo] => 30,27,32,25,26,27<br />        )<br /><br />    [2] => Array<br />        (<br />            [AdminName] => 00001<br />            [MenuNo] => 30,27,32<br />        )<br /><br />)<br />
Copy after login

这在计算方法中称为“聚类”

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template