Share on Google+Share on Google+

How to Append Arrays in PHP

Advertisement
How to Append Arrays PHP: This tutorial is discussed about different techniques to merge two or more than two arrays in PHP e.g. array_merge(), + operator etc. With the help of several examples you can understand the basic functionality of array_merge() function. This tutorial has small description of print_r().

How to append array or merge two arrays in PHP:

This PHP tutorial is discussed about different techniques to merge two or more than two arrays e.g. array_merge(), + operator etc. This free php tutorial has small description of print_r().

In many situations we have to merge two or more than two arrays, we need to use array_merge() function. This function merges or append the elements of one array to the previous array.  

General format of the array_merge() is: array array_merge(array $var1 [, array $var2 [,.....]]), it returns the resulting array.

Unlike in PHP 4, PHP 5 only accepts parameters of array() type. You can typecast to merge different types. This point can be clear with the help of example 4 as mentioned below:  

<?php

echo "<b>EXAMPLE 1 </b><br/>";

$array1= array("name"=> "rose", "age"=>21);

$array2=array("name"=>"india","age"=>22);

//USE array_merge() function

$array1= array_merge(array($array1),array($array2));

//print_r is used to display the information about a variable in human understandable manner

print_r ( $array1);

echo "<br/>";

echo "<br/>";

 echo "<b>EXAMPLE 2 </b><br/>";

$array3=array(2=>2,0=>0,1=>1);

print_r($array3);

$array3=array_merge($array3);

echo "<br/>";

echo "<b> After using array_merge() the index positions will be displayed in sorted manner.</b><br/>";

print_r($array3);

 echo "<br/><br/><b>EXAMPLE 3 </b><br/>";

$var="unknown";

$array4=array(0=>0, 1=>1, array("name"=>"roseindia", "age"=>22));

$array4= array_merge(array($array4),array($var));

print_r($array4);

 echo "<br/><br/><b>EXAMPLE 4 </b><br/>";

$array5=array();

$array6=array(1=>"Good", 2=>"Better",3=>"Best");

$array6= array_merge($array5, $array6);

print_r($array6);

echo "<br/><br/><b>EXAMPLE 4...Continued </b><br/>";

$array7=array();

$array8=array(1=>"Good", 2=>"Better",3=>"Best");

echo"<b>If you want to keep original index position use + operator</b><br/>";

$array8= $array7 + $array8;

print_r($array8);

 ?>

 

Output of the of the above program is:

EXAMPLE 1
Array ( [0] => Array ( [name] => rose [age] => 21 ) [1] => Array ( [name] => india [age] => 22 ) )

EXAMPLE 2
Array ( [2] => 2 [0] => 0 [1] => 1 )
After using array_merge() the index positions will be displayed in sorted manner.
Array ( [0] => 2 [1] => 0 [2] => 1 )

EXAMPLE 3
Array ( [0] => Array ( [0] => 0 [1] => 1 [2] => Array ( [name] => roseindia [age] => 22 ) ) [1] => unknown )

EXAMPLE 4
Array ( [0] => Good [1] => Better [2] => Best )

EXAMPLE 4...Continued
If you want to keep original index position use + operator
Array ( [1] => Good [2] => Better [3] => Best )

 

A small description of print_r() is as follows:

Description

mixed print_r (mixed $var [, boolean $return=false] )

Parameters 

$var: variable or expression to be printed

$return: return type, default value is false

Return Values

string, float, integer, array, object

Advertisements

Advertisement

Posted on: October 16, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.