Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...