Array merge without function in PHP

Array merge without function in PHP: In the article, you going to see the discussion regarding how one individual easily merge array without various different types of function in PHP.

But this article is very much useful for one who looking for answer regarding merging array without function.Try to explain the Users in two different way.

Every User can easily understand with the below given code scripts.

Lets Checkout Source code and Detail Explanation Array merge without function in PHP.

Below is script code for without function. Two methods are used to explain this merging of array. It is explained in a simple way. Lets checkout below.

Method 1 :Array merge PHP

<?php
$arr1 = array(1,2,3,4,5);
$arr2 = array(6,7,8,9,0);
$allarrval = array();
?>

As you see above code scripts consist of two arrays variable assigned set of array value and two array variables are $arr1 and $arr2 and also defined one array variable called $allarrval which contain all the merge value of both array.

In this we going to use foreach key value, in that exactly taking all the value second array in first array.

Using this all the value comes in first. Below is code script with the help of which you can understand easily.

<?php
foreach($arr2 as $key=>$val){
    $arr1[] = $val;
}
?> 

Then final step for get exactly merge value whithout repeatition of value.For this used check using function in array function,below is the code snippets with the help of it you can understand easily.

To check all the array value are merge or not you can use print function to get out put all the merge value.

<?php
foreach($arr1 as $k=>$v){
    if(!in_array($v,$allarrval)){
        $allarrval[] = $v;
    }
}
 print_R($allarrval);

?>
Method one : Output

Method 2 : Array Merge

Now we are going to use second method for merging the array but defined array merge function with the help of full code snippets you can easily understand easily. In this we defined array function we executing two foreach function.

With the help of code snippets you can understand easily.

<?php
function arrayMerge(array $arrays) {
    $mergeArray = [];

    foreach ($arrays as  $key => $array) {
        foreach($array as $finalArray) {
            $mergeArray[] = $finalArray;
        }
    }

   return $mergeArray;
}


$array1 = [
    'Apple', 'Banana', 'Mango'
];

$array2 = [
    'Vegetable', 'Fruit', 'Mix'
];

$array3 = [
    'Potato', 'Tomato', 'Cabbage'
];

print_r(arrayMerge([$array1, $array2, $array3]));
?>
Method Two : Output

Conclusion :

Finally done explanation regarding merging without function in PHP. Please feel free to comment on it if you do not understand it.

Leave a Reply

Your email address will not be published.