PHP Push MultiDimensional Array


 

PHP Push MultiDimensional Array

PHP Push MultiDimensional Array: In this tutorial you learn how to push one multi-dimensional array into another, there are various examples are available which will exemplify.

PHP Push MultiDimensional Array: In this tutorial you learn how to push one multi-dimensional array into another, there are various examples are available which will exemplify.

PHP Push MultiDimensional Array 

This tutorial exemplifies how to push a multi-dimensional array into another array, a single element into an array, and so on. These all process are done by array_push() function.

array_push() is one of the widely used function to add item into an array. General format, parameters it take and type of value it returns is as follows:

General Format

int array_push(array &$array, mixed $var [,mixed ..})

Parameters 

array &$array: Input array

mixed $var: Pushed value

Return Value

No of elements in the array

 

For detail please visit our web page: http://roseindia.net/tutorial/php/phpbasics/PHP-Array-Push.html

PHP Push Multi-Dimensional Array Example :

<?php

$array1=$array3=array(1,2,3,4,5,6,

array('new delhi','mumbai','kolkata'));

echo"<br/><b>Value of original array is:</b><br/>";

print_r($array1);

$array2=array('a','b','c');

array_push($array1,$array2);

echo"<br/><b>After appending an array:</b><br/>";

print_r($array1);

$var="new variable";

echo"<br/><b>After appending a value:</b><br/>";

array_push($array1,$var);

print_r($array1);

echo"<br/><b>After appending to itself :</b><br/>";

array_push($array3,$array3);

print_r($array3);

?>

Output:

Value of original array is:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => Array
        (
            [0] => new delhi
            [1] => mumbai
            [2] => kolkata
        )

)

After appending an array:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => Array
        (
            [0] => new delhi
            [1] => mumbai
            [2] => kolkata
        )

    [7] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)

After appending a value:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => Array
        (
            [0] => new delhi
            [1] => mumbai
            [2] => kolkata
        )

    [7] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [8] => new variable
)

After appending to itself :
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => Array
        (
            [0] => new delhi
            [1] => mumbai
            [2] => kolkata
        )

    [7] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => Array
                (
                    [0] => new delhi
                    [1] => mumbai
                    [2] => kolkata
                )

        )

)

Ads