Course Banner


PHP Array

Array is a group of elements which has common name. Array value can be accessed using it’s index.

Array exists in almost all programming languages. Array provides very powerful tools for manipulating data.

PHP provides a lot of array functions which makes the things easier.

In this tutorial, first we will see types of functions with examples and then we will also have a look at PHP in-built array functions.

Array Types

Types of arrays:

● Indexed array

● Associative Array

● Multidimensional Array

Indexed Array

● Indexed Array

In this kind of array key is hidden and start with sequential numeric index, such as 0,1,2 etc.

Example:

< ?php
  1. $arr = array(1,2,3,4);
  2. //let’s show its value.
  3. print_r($arr);
  4. ?>

Output:

So in actual the above array is like this

$arr = array();
$arr[0] = ‘1’
$arr[1] = ‘2’
$arr[2] = ‘3’
$arr[3] = ‘4’

So, if you want to show value 3 then you have to check its index. And its index is, 2. Because array index always starts with 0.

Also in indexed array, we normally don't define its key.

Example:

< ?php
  1. $arr = array(1,2,3,4);
  2. //let’s show its value.
  3. //print_r ($arr);
  4. echo $arr[2];
  5. ?>

Result

Associative Array

● Associative Array

The array in which user defines its value as well as key. And array key and value can be anything.

Example:

< ?php
  1. $country= array(
  2. 'AF'=>'Afghanistan',
  3. 'AL'=>'Albania',
  4. );
  5. foreach($country as $key=> $value){
  6.  echo$key . " - " .$value. "< br>";
  7. }
  8. ?>

Output :

Multi-Dimensional Array

● Multi-Dimensional Array

In multi-dimensional array value of one array is array itself.

Example:

< ?php
  1. $world_map= array (
  2. array('City', 'Country', 'Continent'),
  3. array('New Delhi', 'India', 'Asia'),
  4. array('Montreal', 'Canada', 'North America'),
  5. array('New York City', 'USA', 'North America'),
  6. array('Mumbai', 'India', 'Asia'),
  7. array('Seoul', 'Korea', 'Asia'),
  8. array('Shanghai', 'China', 'Asia'),
  9. array('Lagos', 'Nigeria', 'Africa'),
  10. array('Buenos Aires', 'Argentina', 'South America'),
  11. array('Cairo', 'Egypt', 'Africa'),
  12. array('London', 'UK', 'Europe')
    );
  13. echo" < pre/> ";
  14. print_r($world_map);
  15. ?>


Structure

Based on above structure if you want to show Canada then you will have to write like this

echo $world_map[2][1];

here is complete code :

< ?php
  1. $world_map=array(
  2. array('City', 'Country', 'Continent'),
  3. array('New Delhi', 'India', 'Asia'),
  4. array('Montreal','Canada', 'North America'),
  5. array('New York City', 'USA', 'North America'),
  6. array('Mumbai', 'India', 'Asia'),
  7. array('Seoul', 'Korea', 'Asia'),
  8. array('Shanghai', 'China', 'Asia'),
  9. array('Lagos', 'Nigeria', 'Africa'),
  10. array('Buenos Aires', 'Argentina', 'South America'),
  11. array('Cairo', 'Egypt', 'Africa'),
  12. array('London', 'UK', 'Europe')
  13. );
  14. //echo "< pre/>";
  15. //print_r ($world_map);
  16. ?>
    < table width="500"border="1">
  17. < tr>
  18. foreach($world_map[0] as $key=> $value){

Output