Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


PHP Arrays

PHP | Values | Operators | Functions | Arrays | Forms | Sessions-Cookies | Classes

An array is like a variable, but can store multiple values in table format. An array has an index and value field. The index field, also known as the key, is the id field. The value field stores a value. By default, an array starts the index with a numeric value of 0, then continues in sequence. The index is declared within  [ ]  square brackets...

<?php

$colors[0]='blue';
$colors[1]='green';

?>

By default, the first index value is zero if the index is not declared...

<?php

$colors[ ]='blue';
$colors[ ]='green';

?>


This array is defined with a starting numeric index of 1...

<?php

$city[1]='Vancouver';
$city[2]='Portland';

?>


An array can be defined with a string index, known as an associative array...

<?php

$city['WA']='Vancouver';
$city['OR']='Portland';

?>


This is an alternative way to declare an array as a function, which turns a variable into an array...

<?php

$colors=array('blue', 'green');

?>

This has the same meaning...

<?php

$colors[0]='blue';
$colors[1]='green';

?>


This array is defined with a starting numeric index of 12. The values are assigned to the index with the  =>  operator...

<?php

$colors=array(12 => 'blue', 'green');

?>


This is an associative array defined with a string index...

<?php

$city=array('WA' => 'Vancouver', 'OR' => 'Portland');

echo $city['WA'];

?>

The output is...

Vancouver




This array is created with a range of values...

<?php

$year=range(2001, 2003);

?>

This has the same meaning...

<?php

$year[0]=2001
$year[1]=2002
$year[2]=2003

?>


The  print_r()  function outputs the values of an array to a browser for testing and formatting purposes...

<?php

$array1['water']='blue';
$array1['land']='green';

$array2[ ]='blue';
$array2[ ]='green';

$array3['water']='blue';
$array3[ ]='green';

print_r($array1);
print_r($array2);
print_r($array3);

?>

The output is...

Array ( [water] => blue [land] => green ) Array ( [0] => blue [1] => green ) Array ( [water] => blue [0] => green )



This has the same meaning...

<?php

$array1=array('water' => 'blue', 'land' => 'green');
$array2=array('blue', 'green');
$array3=array('water' => 'blue', 'green');

print_r($array1);
print_r($array2);
print_r($array3);

?>

The output is...

Array ( [water] => blue [land] => green ) Array ( [0] => blue [1] => green ) Array ( [water] => blue [0] => green )




Values can be added to an existing array...

<?php

$months=array('May', 'June', 'July');
$months[ ]='October';

print_r($months);

?>

The output is...

Array ( [0] => May [1] => June [2] => July [3] => October )




This array is defined with a starting numeric index of 6...

<?php

$months=array(6 => 'May', 7 => 'June', 8 => 'July');

print_r ($months);

?>

The output is...

Array ( [6] => May [7] => June [8] => July )




Employee names can be stored in an array...

<?php

$employee[0]='Person1';
$employee[1]='Person2';
$employee[2]='Person3';
$employee[3]='Person4';

echo 'Two employees are ' . $employee[0] . ' and ' .
     $employee[1] . '.';
echo '<br />Two more are ' . $employee[2] . ' and ' .
     $employee[3] . '.';

?>

The output is...

Two employees are Person1 and Person2.
Two more are Person3 and Person4.





Salaries can be stored in an array...

<?php

$salaries['Person1']=1500;
$salaries['Person2']=1400;
$salaries['Person3']=1300;
$salaries['Person4']=1200;

echo 'Person1 is paid $' . $salaries['Person1'] . '<br />';
echo 'Person2 is paid $' . $salaries['Person2'] . '<br />';
echo 'Person3 is paid $' . $salaries['Person3'] . '<br />';
echo 'Person4 is paid $' . $salaries['Person4'];

?>

The output is...

Person1 is paid $1500
Person2 is paid $1400
Person3 is paid $1300
Person4 is paid $1200





The  foreach()  function evaluates all the values of an array one at a time. The as command generates the statements. The index references $name. The value field references $age...

<?php

$employeeAge;
$employeeAge['Person1']='18';
$employeeAge['Person2']='24';
$employeeAge['Person3']='30';
$employeeAge['Person4']='36';

foreach($employeeAge as $name => $age)
{echo "Name: $name - Age: $age <br />";}

?>

The output is...

Name: Person1 - Age: 18
Name: Person2 - Age: 24
Name: Person3 - Age: 30
Name: Person4 - Age: 36




This is another example of the  foreach()  function...

<?php

$element=array('Land', 'Water', 'Air', 'Energy');

foreach($element as $value)
{echo 'The value is: ' . $value . '<br />';}

?>

The output is...

The value is: Land
The value is: Water
The value is: Air
The value is: Energy





The value field of an array can also store other arrays. This is known as a multidimensional array. By default, each element in a multidimensional array is separated by a  ,  comma and listed in numeric order starting with 1...

<?php

$employee=array(
array('Name' => 'Person1', 'Religion' => 'Christianity'),
array('Name' => 'Person2', 'Religion' => 'Buddhism'),
array('Name' => 'Person3', 'Religion' => 'Islam'),
array('Name' => 'Person4', 'Religion' => 'Judaism')
);

echo $employee[2]['Religion'];

?>

The output is...

Buddhism




The  count()  function adds the number of elements in an array...

<?php

$numbers=array('one', 'two', 'three');
$test=count($numbers);

echo "There are $test numbers.";

?>

The output is...

There are 3 numbers.




The  explode()  function creates an array from a string...

<?php

$array1='dog cat mouse pig cow horse';
$array2=explode(' ', $array1);

print_r($array2);

?>

The output is...

Array( [0] => dog [1] => cat [2] => mouse [3] => pig [4] => cow [5] => horse )




The  implode()  function creates a sentence from an array...

<?php

$array=array('Hello','World!','Goodbye','World!');

echo implode(' ',$array);

?>

The output is...

Hello World! Goodbye World!




SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington