ホームページ >バックエンド開発 >PHPチュートリアル >PHPの二次元配列の扱い方
php 二次元配列
Array ( [0] => Array ( [AdminName] => sysadm [MenuNo] => ) [1] => Array ( [AdminName] => 00000 [MenuNo] = > 30,27,32 ) [2] => 配列 ( [管理者名] => 00000 [メニュー番号] => 25,26,27 ) [3] => 配列 ( [管理者名] => 00001 [MenuNo] => 30,27,32 ) )
配列は上記の通りです: 同じAdminNameをマージし、MenuNoの値を1つのピースにマージする方法:
Array ( [0] =>配列 ( [管理者名] => ; sysadm [メニュー番号] => ) [1] => 配列 ( [管理者名] => 00000 [メニュー番号] => 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));
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 />