ホームページ >バックエンド開発 >PHPチュートリアル >2次元配列のマージに関する質問
有一数组:
配列
(
[0] => 配列
(
[a] => 配列
(
[id] => 4
)
)
[1] =>配列
(
[a] => 配列
(
[id] => 5
(
[id ] => 4
)
)
[3] => 配列 (
[b] => ; 配列
(
[id] => 7
)
)
请问どのように結合するか
Array(
[0]=>4
[1]=>5
[2]=>4
[3]=>7
)
value都是二维数组id
回复讨论(解决方案)
$arr = Array( '0' => Array ( 'a' => Array ( 'id' => 4 ) ), '1' => Array ( 'a' => Array ( 'id' => 5 ) ), '2' => Array ( 'b' => Array ( 'id' => 4 ) ), '3' => Array ( 'b' => Array ( 'id' => 7 ) ));$new_arr = array();foreach($arr as $vl){ foreach($vl as $v) $new_arr[] = $v['id'];}print_r($new_arr);
$a = array ( 0 => array ( 'a' => array ( 'id' => '4', ), ), 1 => array ( 'a' => array ( 'id' => '5', ), ), 2 => array ( 'b' => array ( 'id' => '4', ), ), 3 => array ( 'b' => array ( 'id' => '7', ), ),);foreach($a as $t) $b[] = current(current($t));print_r($b);
Array( [0] => 4 [1] => 5 [2] => 4 [3] => 7)
$a = array ( 0 => array ( 'a' => array ( 'id' => '4', ), ), 1 => array ( 'a' => array ( 'id' => '5', ), ), 2 => array ( 'b' => array ( 'id' => '4', ), ), 3 => array ( 'b' => array ( 'id' => '7', ), ),);$num=sizeof($a);array_walk_recursive($a,'my',$num);function my($v,$k,$num){ static $arr=array(); $arr[]=$v; if(sizeof($arr)==$num){ print_r($arr); }}
Array( [0] => 4 [1] => 5 [2] => 4 [3] => 7)