php tutorial - How to Merge Arrays in PHP - php programming - learn php - php code - php script




  • PHP provides some built-in array functions which help to do array operation so easily. The array_merge() function is one of them.
 merge array in php

Learn PHP Tutorial - PHP Tutorial tutorial - merge array in php - PHP Tutorial examples - PHP Tutorial programs

  • PHP array_merge() function creates a new array merging two or more PHP arrays but the original arrays remain unchanged.
  • The structure of a array_merge() function is given below…
$merged_array = array_merge($array1,$array2…….$arrayN);
click below button to copy the code. php tutorial - team

  • The structure shows that the array_merge() function accepts two or more arrays as arguments and returns another array as a return value but does not change the original arrays.

Sample Code

<?php
 $web_lang = array("PHP","HTML","CSS");
 $apps_lang = array("C++","C#","Java");
 $com_lang = array_merge($web_lang,$apps_lang);
 echo "Merged array's elements : ";
 foreach($com_lang as $lang){
  echo $lang." ";
 }
 echo " First array's elements : ";
 foreach($web_lang as $lang){
  echo $lang." ";
 }
 echo " Second array's elements : ";
 foreach($apps_lang as $lang){
  echo $lang." ";
 }
?>
click below button to copy the code. php tutorial - team

Output

Merged array’s elements: PHP HTML CSS C++ C# Java
First array’s elements: PHP HTML CSS
Second array’s elements: C++ C# Java
  • The example shows that two arrays are merging with the array_merge() function but the original arrays are remaining unchanged.
  • So, multiple PHP arrays can easily be merged using the array_merge() function.

PHP array_combine() function

  • The array_combine() function helps to create a new array combining two arrays where one array elements will be indexes and another array elements will be elements of the new array.
  • But the both arrays must have equal elements.

Sample Code

<?php 
 
$fruits = array("Mango","Apple","Banana"); 
$colors = array("Green","Red","Yellow");
$fruit_color = array_combine($fruits,$colors); 
 
foreach($fruit_color as $key => $value){
   echo $key." is ".$value." ";
}
 
echo "The combined array is: ";
 
print_r($fruits_color);
 
?>
click below button to copy the code. php tutorial - team

Output

Mango is Green
Apple is Red
Banana is Yellow
  • The example shows that the array_combine() function returns an array which indexes are the first array elements and the values are the second array elements.
  • So, two PHP arrays can be combined using the array_combine() function so easily.

Related Searches to How to Merge Arrays in PHP

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×