Core PHP

Initializing Multidimensional Arrays

A multidimensional array holds recursively-defined arrays of arrays of data items. These items are selected and accessed via a sequence of integer indices or string.

The program below creates an two multidimensional arrays. The first array uses the functional notation of create a two-dimensional array using both numbers and strings to access entries in the array. The second array uses the short array notation to initialize a three-dimensional with numerical indicies.

InitializeMultidimensionalArrays.php

<?php

// Using the array function syntax
$saaTwoDimensional = array(
  0 => array('husband' => 'Joseph', 'wife' => 'Mary'),
  1 => array('husband' => 'Abraham', 'wife' => 'Sarah'),
  2 => array('husband' => 'Joachim', 'wife' => 'Anne'),
  3 => array('husband' => 'Isaac', 'wife' => 'Rebecca')
);

echo '<pre>';
print_r($saaTwoDimensional);
echo '</pre>';

// Using the short array syntax
$iaaaThreeDimensional = [
  0 => [0 => [0 => .2, 1 => .5], 1 => [0 => .9, 1 => .4]],
  1 => [0 => [0 => .8, 1 => .3], 1 => [0 => .1, 1 => .7]]
];

echo '<pre>';
print_r($iaaaThreeDimensional);
echo '</pre>';
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.