Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

how to join two multidimensional arrays in php

how to join two multidimensional arrays in php? I have two multidimensional arrays A and B. I need to join A and B to form a new array C as follows

$A = array( 
array("a1"=>1,"b1"=>2,"c1"=>"A"), 
array("a1"=>1,"b1"=>16,"c1"=>"Z"), 
array("a1"=>3,"b1"=>8,"c1"=>"A")); 

$B = array( 
array("a2"=>1,"b2"=>2,"b2"=>"A"), 
array("a2"=>1,"b2"=>16,"b2"=>"G"), 
array("a2"=>3,"b2"=>8,"b2"=>"A")); 

//join A and B to form C

$C=array( 
array("a1"=>1,"b1"=>2,"c1"=>"A"), 
array("a1"=>1,"b1"=>16,"c1"=>"Z"), 
array("a1"=>3,"b1"=>8,"c1"=>"A"),
array("a2"=>1,"b2"=>2,"b2"=>"A"), 
array("a2"=>1,"b2"=>16,"b2"=>"G"), 
array("a2"=>3,"b2"=>8,"b2"=>"A"));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the array_merge function, like this:

$C = array_merge($A, $B);
print_r($C);

When I run the above script it'll output:

Array ( 
    [0] => Array ( 
        [a1] => 1 
        [b1] => 2 
        [c1] => A 
        ) 
        [1] => Array ( 
            [a1] => 1 
            [b1] => 16 
            [c1] => Z ) 
        [2] => Array ( 
            [a1] => 3 
            [b1] => 8 
            [c1] => A 
        ) 
        [3] => Array ( 
            [a2] => 1 
            [b2] => A
        ) 
        [4] => Array ( 
            [a2] => 1 
            [b2] => G 
        ) 
        [5] => Array ( 
            [a2] => 3 
            [b2] => A 
        )
    ) 

Take a quick read here: http://php.net/manual/function.array-merge.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...