Arrays

Data structure that stores one or more similar data type values in a single variable

 

Data is stored in elements as key / value pairs. The key is also known as the index

 

3 different types of arrays:

  • Numeric array - Utilises a numeric index. Values are stored and accessed in linear fashion
  • Associative array - Utilises strings as index. Values stored in association with key (string) values
  • Multidimensional array - An array containing one or more arrays. Values accessed using multiple indices

Use the array() construct to create an array. Values are automatically assigned to a numerical index, if not specifically defined:

 

array("model", "price", "speed") ;

 

Alternatively, the Big Arrow => notation can be used:

 

array("0" => "model", "1" => "price", "2" => "speed") ;

 

*note: it is possible to assign alternative key values, as long as they are unique, otherwise their values will be overwritten

 

 

Numeric Array

 

Keys start from 0

 

akin to a spreadsheet that has just one column

 

Assigned using the array(); function:

<?php
	$myArray = array("dave@home.com", "andy@home.com", "sue@home.com", "fred@home.com");

	while (list($key,$value) = each($myArray)) {
	echo "The element at Key position $key has a value of $value<br>";
	}

	//to access a specific element, simply use its key
	echo $myArray[2];
?>

 

or individually assigning specific elements, using the [ ] (aka Array Operator) notation:

<?php
	$myArray[0] = "dave@home.com";
	$myArray[1] = "andy@home.com";
	$myArray[2] = "sue@home.com";
	$myArray[ ] = "fred@home.com"; //this last assignment will go into the next highest: [3]

	while (list($key,$value) = each($myArray)) {
        print "Key: $key = $value<br>";
    }
?>

 

 

Associative Array

 

Keys must be unique strings

 

akin to a spreadsheet that has just one column and uses named cells for reference (instead of a cell number)

 

<?php
	$team = array('Yamaha' => "Rossi", 'Ducati' => "Hayden", 'Honda' => "Pedrosa") ;

	while (list($key,$value) = each($team)) {
        print "$value rides for $key<br>";
    }
?>

or individually assigning specific elements, using the [ ] notation:

<?php
	$team[Yamaha] = 220;
	$team["Ducati"] = 225 ;
	$team["Honda"] = 223 ;

	while (list($bike,$speed) = each($team)) {
        print "The $bike has a top speed of: $speed<br>";
    }
?>

 

Multidimensional Arrays

 

An array of arrays

Each element in the array can be an array, and each element in that sub-array can be an array, etc.

<?php
	$motoGPteam['Yamaha'] = array("Rider"=>"Lorenzo", "Position"=>1, "Nationality"=>"Spanish");
	$motoGPteam['Honda'] = array("Rider"=>"Marquez", "Position"=>1, "Nationality"=>"Spanish");
	$motoGPteam['Ducati'] = array("Rider"=>"Hayden", "Position"=>9, "Nationality"=>"American");
	$motoGPteam['ART'] = array("Rider"=>"De Puniet", "Position"=>13, "Nationality"=>"French");

	echo "<pre>" ; //to get a better formatted display of the output
	var_dump($motoGPteam);
	echo "<pre>" ;
?>

Displays:

array(4) {
   ["Yamaha"]=>
   array(3) {
      ["Rider"]=>
      string(7) "Lorenzo"
      ["Position"]=>
      int(1)
      ["Nationality"]=>
      string(7) "Spanish"
   }
   ["Honda"]=>
   array(3) {
      ["Rider"]=>
      string(7) "Marquez"
      ["Position"]=>
      int(1)
      ["Nationality"]=>
      string(7) "Spanish"
   }
   ["Ducati"]=>
   array(3) {
      ["Rider"]=>
      string(6) "Hayden"
      ["Position"]=>
      int(9)
      ["Nationality"]=>
      string(8) "American"
   }
   ["ART"]=>
   array(3) {
      ["Rider"]=>
      string(9) "De Puniet"
      ["Position"]=>
      int(13)
      ["Nationality"]=>
      string(6) "French"
   }
}

Leave a Reply