Arrays are kings of advanced data structure in PHP, extremely flexible and can do practically anything we can possibly imagine. As variable, an array is a special variable because an array is a data structure that stores one or more values in a single value.

All arrays are ordered collections of items, it’s called elements. Each element has a value that is identified by a key that is unique to the array it belongs to, it’s called index. So, each element in the array has it’s own index that it can be easily accessed.

1
2
3
4
5
6
7
8
	$var_array = array(100, 200, 300, 400, 500);
	$var_array = array(
				‘a’	=> 100,
				‘b’	=> 200,
				‘c’	=> 300,
				‘d’	=> 400,
				‘e’	=> 500
			       );

 

In the first line of code, we creates an array by only specific values of the elements, the index are automatically assigned with a numeric to each element, starting from zero.

In the second line of code, the index are specified in the call to array or we assign the index manually.

In PHP, there area three kind of arrays : Numeric array - an array with a numeric index, Associative array – an array where each ID key is associated with a value and Multidimensional array – an array containing one or more arrays. In the example above, first line of code is numeric array and the second line of code is associative array.

 

 

Numeric Arrays

There are two methods that we can use to create a numeric array, the index are automatically assigned with start at zero and the index are assigned manually, see following example for detail.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
        /*
         *   Example of numeric array with automatically index assign
         */
 
	     $mybags = array(“Eiger”, “Exports”, “Polo”);
 
	/*
         *   Example of numeric array with manually index assign
         */
	     $mycars[0] = “Jaguar”;
	     $mycars[1] = “BMW”;
	     $mycars[2] = “Honda”;
 
       /*
        *  Example to access the variable array
        */
 
            echo ‘My favorite Bag is :.$mybags[1].’ and My favorite Car is :.$mycars[2];

 

 

 

Associative Arrays

In the associative arrays, we can use the values as keys and assign value to them. See following example, how create and access the associative array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
         /*
          * Create Associative Array
          */
	        $fav_employee = array(
					         “MIS”	=> “Adolf Hittler”,
					         “Accounting”	=> “Franklin D “,
					         “PPIC”	=> “B Obama”
				              );
 
          /*
           * Access the Associative array
           */
                echo “The best employee is ‘.$fav_employee[‘PPIC’];

 

 

 

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. See following example of the multidimensional arrays.

1
2
3
4
5
6
7
8
9
10
11
12
	$array = array();
        $array[] = array(
                               ’foo’,
                               ’bar’
                             );
 
        $array[] = array(
                               ’baz’,
                               ’bat’
                             );
 
        echo $array[0][1] . $array[1][0];
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Slashdot
  • Technorati
  • YahooMyWeb