PHP Arrays

PHP Arrays

An array is a data structure that stores one or more values in a single variable. Each element in the array has its own id. There are 3 types of arrays in PHP namely,:

  • Numeric Array: An array with a numeric ID key.
  • Associative Array: An array where each ID key is associated with a value.
  • Multidimensional Array: An array containing one or more arrays.

Let’s see each of these arrays.


Numeric array

Whenever some data is stored in an array, some ID is assigned to each value. In numeric array, the ID assigned to each value is numeric. Numeric arrays can be created in many ways. Let’s see those ways.

//  Method I
  $name = array('Value1','Value2','Value3' );
//  Method II
  $name[] = 'Value1';
  $name[] = 'Value2';
  $name[] = 'Value3';
//  Method III
  $name[0] = 'Value1';
  $name[1] = 'Value2';
  $name[2] = 'Value3';

Let’s see each of these ways.

<?php
//  Method I
  $name = array( 'Mike','Scott','Peter' );
  echo $name[0].", ".$name[1].", ".$name[2];
?>

In the first method, the keys are assigned automatically but remember that the keys start from 0 and not from 1.

<?php
//  Method II
  $name[] = 'Mike';
  $name[] = 'Scott';
  $name[] = 'Peter';
  echo $name[0].", ".$name[1].", ".$name[2];
?>

In the second method too, the keys are assigned automatically and the keys start from 0.

<?php
//  Method III
  $name[0] = 'Mike';
  $name[1] = 'Scott';
  $name[2] = 'Peter';
  echo $name[0].", ".$name[1].", ".$name[2];
?>

In the last and the third method, we have assigned the keys ourselves. The output of all the three methods will be the same as given below:

Mike, Scott, Peter

Below is an example to make arrays more clear:

<?php
	echo "looping through array";
	$x = array(0,1,4,9,16,25,36,49,64,81);
	for($j = 0;$j<10; $j++)
	{
	    echo " $j  : $x[$j]";
	}
?>

and the output will be:

looping through array
0
1
4
9
16
25
36
49
64
81

Associative Array

In associative array, the ID key is associated with a value. There are two ways of creating an associative array.

//  Method I
  $name = array("key1"=>Value1, "key2"=>Value2, "key3"=>Value3);
//  Method II
  $name['key1'] = "Value1";
  $name['key2'] = "Value2";
  $name['key3'] = "Value3";

Let’s see both these methods.

<?php
     $mdays = array("January" => 31,
		    "February" => 28,
		    "March" => 31,
		    "April" => 30,
		    "May" => 31,
		    "June" => 30,
		    "July" => 31,
		    "August" => 31,
		    "September" => 30,
		    "October" => 31,
		    "November" => 30,
		    "December" => 31
		   );
     echo "February has ".$mdays['February']." days.";
?>

Now let’s see the second method.

<?php
       $mdays['January'] = "31";
       $mdays['February'] = "28";
       $mdays['March'] = "31";
       $mdays['April'] = "30";
       $mdays['May'] = "31";
       $mdays['June'] = "30";
       $mdays['July'] = "31";
       $mdays['August'] = "31";
       $mdays['September'] = "30";
       $mdays['October'] = "31";
       $mdays['November'] = "30";
       $mdays['December'] = "31";
    echo "February has ".$mdays['February']." days.";
?>

The output of both the methods will be:

February has 28 days.

Multidimensional Array

In a multidimensional array each element in the main array can also be an array. A simple multidimensional array is normally visualised as a matrix. Let’s see the syntax to create a simple multidimensional array.

$name = array
(
    "array1" => array
    (
        "value1",
        "value2",
        "valuen"
    ),
    "array2" => array
    (
        "value1",
        "valuem
    ),
    "arrayz" => array
    (
        "value1",
        "valuey"
    )
);

Below is an example to make multidimensional arrays more clear:

<?php
$cds = array
(     "Christina Aguilera" => array
      (
           "Impossible",
           "Beautiful",
           "Dirrty"
      ),
      "Kelly Rowland" => array
      (
           "Stole",
           "Obsession"
      )
);
?>

To access the first cd of “Christina Aguilera”, the PHP script will be:

<?php
echo "First cd of Christina Aguilera is ".$cds['Christina Aguilera'][0];
?>

and the output will be:

First cd of Christina Aguilera is Impossible

Previous Post

Next Post

Comments are closed.