ホームページ >バックエンド開発 >PHPチュートリアル >質問は少し複雑ですので、めまいがする場合は入力しないでください^_^
array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'),
1 => 配列 ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>2)、
2 => ; 配列 ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=> '1')、
3 => ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>2'),
)
type=1 の場合は削除されます同じidを繰り返してキーがIDとなり、type=2の場合、KEYはid_bianの形となり、最終的には次のような配列
array ( 0 => array ( '10' => '', '11_1) に変換されます。 ' => '','11_2' => ''),)
それを次の配列とマージします
array ( '10' => '1','11_2' => ; '1') 、
1 => 配列 ('10' => '1','11_2' => '1')、
2 => 配列 ('11_1' => '1' ,'11_2' = > '1'),
3 => array ( '10' => '2','11_2' => '1'),
) 最後に
array ( 0 => 配列 ( ' 10' => '1','11_1'=>'', '11_2' => '1')、
1 => 配列 ('10' => '1 ', '11_1 => '', 11_2' => '1'),
2 => 配列 ( '10' => '1', '11_2' = > '1') ,
3 => 配列 ( '10' => '2', '11_1' => '', '11_2' => '1'),
ディスカッションへの返信 (解決策)
$a = array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'), 1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'), 2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'), 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'), );$b = array ( 0 => array ( '10' => '1','11_2' => '1'), 1 => array ( '10' => '1','11_2' => '1'), 2 => array ( '11_1' => '1','11_2' => '1'), 3 => array ( '10' => '2','11_2' => '1'), );$t = array();foreach($a as $item) { if($item['type'] == '1') { $t[$item['id']] = ''; }else $t[$item['id'].'_'.$item['bian']] = '';}$c = array();foreach($b as $v) { $r = array(); foreach($t as $k=>$n) $r[$k] = @$v[$k]; $c[] = $r;}print_r($c);
Array( [0] => Array ( [10] => 1 [11_1] => [11_2] => 1 ) [1] => Array ( [10] => 1 [11_1] => [11_2] => 1 ) [2] => Array ( [10] => [11_1] => 1 [11_2] => 1 ) [3] => Array ( [10] => 2 [11_1] => [11_2] => 1 ))
$a = array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'), 1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'), 2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'), 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'), );$b = array ( 0 => array ( '10' => '1','11_2' => '1'), 1 => array ( '10' => '1','11_2' => '1'), 2 => array ( '11_1' => '1','11_2' => '1'), 3 => array ( '10' => '2','11_2' => '1'), );$t = array();foreach($a as $item) { if($item['type'] == '1') { $t[$item['id']] = ''; }else $t[$item['id'].'_'.$item['bian']] = '';}$c = array();foreach($b as $v) { $r = array(); foreach($t as $k=>$n) $r[$k] = @$v[$k]; $c[] = $r;}print_r($c);
Array( [0] => Array ( [10] => 1 [11_1] => [11_2] => 1 ) [1] => Array ( [10] => 1 [11_1] => [11_2] => 1 ) [2] => Array ( [10] => [11_1] => 1 [11_2] => 1 ) [3] => Array ( [10] => 2 [11_1] => [11_2] => 1 ))
$t が $C の要素として追加された場合、再度トラバースする必要がありますか?
配列 ( '10' => '', '11_1' => '', '11_2' => '') を $c として追加しますわかります意味がよくわかりません
$a = array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'), 1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'), 2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'), 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'), );$b = array ( 0 => array ( '10' => '1','11_2' => '1'), 1 => array ( '10' => '1','11_2' => '1'), 2 => array ( '11_1' => '1','11_2' => '1'), 3 => array ( '10' => '2','11_2' => '1'), );$t = array();foreach($a as $item) { if($item['type'] == '1') { $t[$item['id']] = ''; }else $t[$item['id'].'_'.$item['bian']] = '';}$c = array();foreach($b as $v) { $r = array(); foreach($t as $k=>$n) $r[$k] = @$v[$k]; $c[] = $r;}print_r($c);は、配列 a の結果全体を $c に挿入することです $c を 1 回走査する必要がありますか?