Array push with nested array elements

We are declaring an array and then in this array, we are pushing a 3 element array in which the 1st array has one element, in second element there is an array of 3 elements and then in third array element there is again a single array element.

Example

<?php

$theNestedArray = array ();

$theArr2 = array (
		"theMainProduct" => array (
				12 
		),
		"theExtraElements" => array (
				1,2,9 
		),
		"customComments" => array (
				"comment line 1" 
		) 
);

array_push ( $theNestedArray, $theArr2 ); // PUSHED ONE ELEMENT
var_dump ( $theNestedArray );

echo "<br><br>";

$theArr2 = array (
		"theMainProduct" => array (
				12 
		),
		"theExtraElements" => array (),
		"customComments" => array (
				"the comment line 2" 
		) 
);

array_push ( $theNestedArray, $theArr2 ); // PUSHED SECOND ELEMENT
var_dump ( $theNestedArray );

?>

Output

array (size=1)
  0 => 
    array (size=3)
      'theMainProduct' => 
        array (size=1)
          0 => int 12
      'theExtraElements' => 
        array (size=3)
          0 => int 1
          1 => int 2
          2 => int 9
      'customComments' => 
        array (size=1)
          0 => string 'comment line 1' (length=14)



array (size=2)
  0 => 
    array (size=3)
      'theMainProduct' => 
        array (size=1)
          0 => int 12
      'theExtraElements' => 
        array (size=3)
          0 => int 1
          1 => int 2
          2 => int 9
      'customComments' => 
        array (size=1)
          0 => string 'comment line 1' (length=14)
  1 => 
    array (size=3)
      'theMainProduct' => 
        array (size=1)
          0 => int 12
      'theExtraElements' => 
        array (size=0)
          empty
      'customComments' => 
        array (size=1)
          0 => string 'the comment line 2' (length=18)
Share

You may also like...