Take 4 arrays then margin the arrays using array_combine function (PHP Array Functions).

 Take 4 arrays then margin the arrays using array_combine function (PHP Array Functions).

STEP 1: First of all we cannot use array_combine() function for 4 arrays because array_combine() function returns associative array where it will return keys and values and the keys and values size should be of same size and as array_combine() function can merge only two arrays into one array, but this array cannot merge more than two array and the size of the two arrays should be of same size. So, here as we have 4 arrays, so we need to use array_merge() function which can merge the arrays one after another and it returns index or numerical array and it can merge more than two arrays and the size of the array can also be different. 

STEP 2: So, here I declared 4 arrays as shown below.

$a1=array('Shounak','Arijit','Ashish');

$b1=array('Rakesh','Shubham','Partha','Sayan');

$c1=array('Naresh','Abhishek','Narayana','Akhila');

$d1=array('Tejas','Avanish','Prashanti');

STEP 3: Then I have use two array_merge function to merge. One is $mer1 to merge $a1 and $b1 and another is $mer2 to merge $c1 and $d1. Then I have print the two merge function $mer1 and $mer2. So the Output will be the merge result of $a1 and $b1 and $c1 and $d1.

$mer1=array_merge($a1,$b1);

$mer2=array_merge($c1,$d1);

print_r($mer1);

echo "<br>";

print_r($mer2); 

Output: 

Array ( [0] => Shounak [1] => Arijit [2] => Ashish [3] => Rakesh [4] => Shubham [5] => Partha [6] => Sayan )
Array ( [0] => Naresh [1] => Abhishek [2] => Narayana [3] => Akhila [4] => Tejas [5] => Avanish [6] => Prashanti )

STEP 4: Here I have used "echo "<br>";" where echo is used to print and br tag is used to provide the output in next line i.e. in new line.

STEP 5: Then I have used array combine to combine the both merged array function. One is $mer1 and another is $mer2. In array_combine function, the array which we will declare first will represent the key and the array which we will declare second will represent the value. So, here I have combined both the merged function $mer1 and $mer2 using array_combine() function and then I have print the array combine function. 

$com=array_combine($mer1,$mer2);

print_r($com);

Output: 

Array ( [Shounak] => Naresh [Arijit] => Abhishek [Ashish] => Narayana [Rakesh] => Akhila [Shubham] => Tejas [Partha] => Avanish [Sayan] => Prashanti )

Code:

<?php

$a1=array('Shounak','Arijit','Ashish');

$b1=array('Rakesh','Shubham','Partha','Sayan');

$c1=array('Naresh','Abhishek','Narayana','Akhila');

$d1=array('Tejas','Avanish','Prashanti');

$mer1=array_merge($a1,$b1);

$mer2=array_merge($c1,$d1);

print_r($mer1);

echo "<br>";

print_r($mer2);

echo "<br>";

$com=array_combine($mer1,$mer2);

print_r($com);

?>

Output:

Array ( [0] => Shounak [1] => Arijit [2] => Ashish [3] => Rakesh [4] => Shubham [5] => Partha [6] => Sayan )
Array ( [0] => Naresh [1] => Abhishek [2] => Narayana [3] => Akhila [4] => Tejas [5] => Avanish [6] => Prashanti )
Array ( [Shounak] => Naresh [Arijit] => Abhishek [Ashish] => Narayana [Rakesh] => Akhila [Shubham] => Tejas [Partha] => Avanish [Sayan] => Prashanti )



Comments

Popular posts from this blog

My Notes On Some Basic Interview Questions Based On PHP