How to concatenate two or more arrays in PHP?

This is one of the example which are simple enough to implement but sometimes we make so many efforts to achieve this but we didn’t succeed at the end. I have suffered with this kind of problem thats why am going to post the entire code to implement array concatenation. You can use implode and explode functions at numerous places but not at all places to make array concatenation look at the following example.

What We want to achieve?

S.No.            Student’s Name                Roll No.
1                   Sudhanshu                        1
2                   Rohin                                2

To show or get output like this we can code in the following way:

$name1 = array();
$
name1["S.No."] = 1;
$
name1["Student's Name"] = 'Sudhanshu';
$
name1["Roll No."] = 1;

$name2 = array();
$
name2["S.No."] = 2;
$
name2["Student's Name"] = 'Rohin';
$
name2["Roll No."] = 2;

$all
[] = $name1;
$all
[] = $name2;
 
 
Now with PHP Code: print_r($all);
We will get output like this: 
 
Array
(
[0] => Array
(
[S.No.] => 1
[Student's Name] => Sudhanshu
[Roll No.] => 1
)

[1] => Array
(
[S.No.] => 2
[Student's Name] => Rohin
[Roll No.] => 2
)

)
 
and print_r($all[0]) will give output as:
 
 Array
(
[S.No.] => 1
[Student's Name] => Sudhanshu
[Roll No.] => 1
)
 
and print_r($all[1]) will give output as: 
 
 Array
(
[S.No.] => 2
[Student's Name] => Rohin
[Roll No.] => 2
)
 
and 
                    foreach($all AS $k=>$v){
foreach($v AS $key=>$nv){
echo $nv;
echo " ";
}
echo "
";
}
will produce output as:
1   Sudhanshu   1   
2 Rohin 2
 
 
To get exact output as top most do the following code:
  


$name=array();
$name[]="S.No.";
$name[]="Student's Name";
$name[]="Roll No.";

$name1 = array(); 
$name1[] = 1; 
$name1[] = 'Sudhanshu'; 
$name1[] = 1; 

$name2 = array(); 
$name2[] = 2; 
$name2[] = 'Rohin'; 
$name2[] = 2; 
 

$all[] = $name;
$all[] = $name1;
$all[] = $name2;

echo "" ;
foreach($all AS $k=>$v){
echo "
";
foreach($v AS $key=>$nv){
echo "";
}
echo "
";
}
echo "

".$nv."

";
 
Output:
S.No. Student’s Name Roll No.
1 Sudhanshu 1
2 Rohin 2