Array Functions in PHP: PART 8

Introduction

In this article I describe the PHP array functions array_merge, array_merge_recursive and array_multisort. To learn some other math functions, go to:

  1. Array Functions in PHP: PART 1
     
  2. Array Functions in PHP: PART 2
     
  3. Array Functions in PHP: PART 3
     
  4. Array Functions in PHP: PART 4
     
  5. Array Functions in PHP: PART 5
     
  6. Array Functions in PHP: PART 6
     
  7. Array Functions in PHP: PART 7

PHP array_merge function

The PHP array_merge function merges one or more arrays into a single array.

Syntax

array_merge(array1, array2,.......................)

Parameters

The parameters in the array_merge function are:

Parameter Description
array1 It specifies an array.
array2 It specifies an array.

Example

An example of the array_merge function is:


<?php
$
array1 = array(1 => "Apple",  2=> "Mango", 3 => "Orange", "Banana");
$array2 = array(1 => "Apple", 2=>"Raspberry", "banana",4 => "Orange");
echo "
<pre>";
$result = array_merge($array1, $array2);
print_r($result);

?>

Output

array-merge-function-in-php.jpg 

PHP array_merge_recursive function

The PHP array_merge_recursive function merges two or more arrays recursively. The difference between the function array_merge and the function array_merge_recursive is that, when two or more array elements have the same key, instead of overriding the keys, the array_merge_recursive() function converts the value into an array.

Syntax

array_merge_recursive(array1, array2,.......................)

Parameters

The parameters in the array_merge_recursive function are:

Parameter Description
array1 It specifies an array.
array2 It specifies an array.

Example

An example of the array_merge_recursive function is:


<?php
$
array1 = array(1 => "Apple",  2=> "Mango", 3 => "Orange");
$array2 = array(1 => "Apple", 2=>"Raspberry", 3=>"banana");
echo "
<pre>";
$result = array_merge_recursive($array1, $array2);
print_r($result);

?>

Output

array-merge-recursive-function-in-php.jpg

PHP array_multisort Function


The PHP array_multisort function is used to sort multiple or multidimensional arrays and return true on success and false on failure.

Syntax

array_multisort(array1,sorting order, sorting type,array2, array3,.........................)

Parameters

The parameters in the array_multisort function are:

Parameter Description
array1 It specifies an array.
sorting order It specifies the sorting order, and order may be:
  • Sort ASC: It is default order in other words by default sort in ascending order.
  • Sort DESC: Sort in descending order.
sorting type It specifies the sorting types, and possible values are:
  • SORT_REGULAR It compare elements normally and it is by default.
  • SORT_NUMERIC It compare elements as numeric value.
  • SORT_STRING     It compare elements as string value.
array2 It specifies an array.
array3 It specifies an array.

Example

An example of the array_multisort function is:


<?
php
$
array1 = array("Mango","Orange","Apple");
$array2 = array("
Orange","Raspberry","banana");
echo "<
pre>";
array_multisort($array1, $array2);
print_r($array1);
print_r($array2);

?>


Output

array-multisort-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all