CopyPastehas never been so tasty!

Accessing elements of multi-dimensional array in PHP

by anonymous

  • 0
  • 0
  • 0
74 views

http://codingvilla.com/accessing-elements-multidimensional-array-php-single-article-661.aspx Arrays in php: Arrays are just like the variable that store values but arrays can be used to store more than one value. Variable only store one value. There are three types of arrays · Numeric arrays · Associative array · Multidimensional array Multidimensional arrays in php: Multidimensional is a type of array that contains one or more arrays. In this type of array each element in the main array can also be an array & each element in the sub-array can be an array. Access elements of multi-dimensional array in PHP: To access the element of a multi-dimensional array in php we use its indexes to access the elements. Index is just like the pointer that points to a particular position of an element. We use indexes to accessing the elements of a multi-dimensional array. In the given code we have accessed the element of multi-dimensional array with the help of key and index. Code: <?php $meals = array('breakfast' => array('A','C'), 'lunch' => array('B', 'E'), 'snack' => array('C','F')); $lunches = array( array('Chicken','Eggplant','Rice'), array('Eggplant','Tofu')); $flavors = array('Japanese' => array('hot' => '1', 'salty' => '2'), 'Chinese' => array('hot' => '3', 'pepper-salty' => '4')); print $meals['lunch'][1]; echo"<br>"; print $meals['snack'][0]; echo"<br>"; print $lunches[0][0]; echo"<br>"; print $lunches[0][1]; echo"<br>"; print $flavors['Japanese']['salty']; echo"<br>"; print $flavors['Chinese']['hot']; ?> Above is a simple code to access the element of multi-dimensional array in php. This simple article tells that how we can access the element of multi-dimensional array in php.

Add A Comment: