An array is a variable that is a set of other variables. The different values of the set can be called using the array name and correct syntax. The easiest way to understand arrays is to see examples of how they are initialized and used.

Suppose we want an array of the days of the week. We can create such an array in the following ways (both work equally well):

$daysOfWeek[0] = "Sunday";
$daysOfWeek[1] = "Monday";
$daysOfWeek[2] = "Tuesday";
$daysOfWeek[3] = "Wednesday";
$daysOfWeek[4] = "Thursday";
$daysOfWeek[5] = "Friday";
$daysOfWeek[6] = "Saturday";


$daysOfWeek = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

As you might expect from looking at the first method, the way you retrieve the values from an array is to code the array name followed by square brackets with the array index in the brackets. Thus:

<?php $today = date('j F Y'); ?> Today's date is: <?php echo $today; ?><br /><br /> The URL for this page is: <?php // This comment won't appear on the page or affect the code in any way echo 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]; echo "<br /><br />"; $nameLast = "Jones"; $nameFirst = "Mark"; $nameMI = "F"; $fullName = $nameFirst . " " . $nameMI . ". " . $nameLast; echo $fullName; echo "<br /><br />"; $daysOfWeek[0] = "Sunday"; $daysOfWeek[1] = "Monday"; $daysOfWeek[2] = "Tuesday"; $daysOfWeek[3] = "Wednesday"; $daysOfWeek[4] = "Thursday"; $daysOfWeek[5] = "Friday"; $daysOfWeek[6] = "Saturday"; echo $daysOfWeek[6]; ?>

which displays thus:

Today's date is: 13 July 2024

The URL for this page is:

Mark F. Jones


Note that an array index starts at 0, not 1. "Sunday" is day 0 above.

A popular form of an array is called an associative array. Here, instead of numeric values for indices, you can use strings. Here's an example:

<?php $today = date('j F Y'); ?> Today's date is: <?php echo $today; ?><br /><br /> The URL for this page is: <?php // This comment won't appear on the page or affect the code in any way echo 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]; echo "<br /><br />"; $nameLast = "Jones"; $nameFirst = "Mark"; $nameMI = "F"; $fullName = $nameFirst . " " . $nameMI . ". " . $nameLast; echo $fullName; echo "<br /><br />"; $daysOfWeek[0] = "Sunday"; $daysOfWeek[1] = "Monday"; $daysOfWeek[2] = "Tuesday"; $daysOfWeek[3] = "Wednesday"; $daysOfWeek[4] = "Thursday"; $daysOfWeek[5] = "Friday"; $daysOfWeek[6] = "Saturday"; echo $daysOfWeek[6]; echo "<br /><br />";
$states_arr = array( 
  "Alabama"=>"AL", "Alaska"=>"AK", "Arizona"=>"AZ", "Arkansas"=>"AR",
  "California"=>"CA", "Colorado"=>"CO", "Connecticut"=>"CT", 
  "Delaware"=>"DE", "Washington DC"=>"DC", "Florida"=>"FL", 
  "Georgia"=>"GA", "Guam"=>"GU", "Hawaii"=>"HI", "Idaho"=>"ID", 
  "Illinois"=>"IL", "Indiana"=>"IN", "Iowa"=>"IA", "Kansas"=>"KS", 
  "Kentucky"=>"KY", "Louisiana"=>"LA", "Maine"=>"ME", "Maryland"=>"MD", 
  "Massachusetts"=>"MA", "Michigan"=>"MI", "Minnesota"=>"MN", 
  "Mississippi"=>"MS", "Missouri"=>"MO", "Montana"=>"MT", 
  "Nebraska"=>"NE", "Nevada"=>"NV", "New Hampshire"=>"NH", 
  "New Jersey"=>"NJ", "New Mexico"=>"NM", "New York"=>"NY", 
  "North Carolina"=>"NC", "North Dakota"=>"ND", "Ohio"=>"OH", 
  "Oklahoma"=>"OK", "Oregon"=>"OR", "Pennsylvania"=>"PA", 
  "Puerto Rico"=>"PR", "Rhode Island"=>"RI", "South Carolina"=>"SC",  
  "South Dakota"=>"SD", "Tennessee"=>"TN", "Texas"=>"TX", "Utah"=>"UT", 
  "Vermont"=>"VT", "Virgin Islands"=>"VI", "Virginia"=>"VA", 
  "Washington"=>"WA", "West Virginia"=>"WV", "Wisconsin"=>"WI", 
  "Wyoming"=>"WY" );
echo $states_arr['Washington DC'];

which displays thus:

Today's date is: 13 July 2024

The URL for this page is:

Mark F. Jones



Array Functions

As with Strings, PHP has many built-in functions to manipulate arrays. Here are some of the more popular ones:


Counts the number of elements in the array.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(count($squadron)); yields 4


Returns the sum of values in an array as an integer or float.
$earnings = array(50,100,150,'Fred');
print_r(array_sum($earnings)); yields 300


Returns an array using the values of the input array as keys and their frequency in input as values.
$array = array(100, "Fred", 100, "Fred", "Fred", 10, 1, "Susan", 10);
print_r(array_count_values($array)); yields
Array ( [100] => 2 [Fred] => 3 [10] => 2 [1] => 1 [Susan] => 1 ) 1;


Checks if a value exists in an array and returns TRUE (1) if it is found in the array, FALSE (0) otherwise.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
echo in_array("SSgt",$squadron); yields 1 (which means 'true').


Sorts an array such that array indices maintain their correlation with the array elements they are associated with. The array is alphabetically sorted by elements.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
asort($squadron); print_r($squadron); yields
Array ( [Williams] => Capt [Adams] => LtCol [Smith] => MSgt [Jones] => SSgt )

Note the array is sorted alphabetically by element (Capt, LtCol, etc).


Sort an array in reverse order and maintain index association.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
arsort($squadron); print_r($squadron); yields
Array ( [Jones] => SSgt [Smith] => MSgt [Adams] => LtCol [Williams] => Capt )
Note the array is sorted in reverse alphabetical order by element.


Sorts an array by key, maintaining key to data correlations.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
ksort($squadron); print_r($squadron); yields
Array ( [Adams] => LtCol [Jones] => SSgt [Smith] => MSgt [Williams] => Capt )
Note the array is sorted in alphabetical order by key.


Sorts an array by key in reverse order, maintaining key to data correlations.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
krsort($squadron); print_r($squadron); yields
Array ( [Williams] => Capt [Smith] => MSgt [Jones] => SSgt [Adams] => LtCol )

As expected, the array is sorted in reverse alphabetical order by key.


Pops and returns the last value of the array, shortening the array by one element. If array is empty (or is not an array), NULL will be returned.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_pop($squadron)); yields LtCol and print_r($squadron); now yields
Array ( [Williams] => Capt [Smith] => MSgt [Jones] => SSgt )


Shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_shift($squadron)); yields MSgt and print_r($squadron); now yields
Array ( [Jones] => SSgt [Williams] => Capt [Adams] => LtCol )


Returns the keys, numeric and string, from the input array.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_keys($squadron)); yields Array ( [0] => Smith [1] => Jones [2] => Williams [3] => Adams )


Returns all the values from the input array and indexes numerically the array.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_values($squadron)); yields Array ( [0] => MSgt [1] => SSgt [2] => Capt [3] => LtCol )


Returns an array in flip order, i.e. keys from $array become values and values from $array become keys.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_flip($squadron)); yields
Array ( [MSgt] => Smith [SSgt] => Jones [Capt] => Williams [LtCol] => Adams )


Return an array with elements in reverse order.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_reverse($squadron)); yields
Array ( [Adams] => LtCol [Williams] => Capt [Jones] => SSgt [Smith] => MSgt )


Removes duplicate values from an array.
$ranks = array("Maj","MSgt","Capt","MSgt","Capt","Capt","SSgt");
print_r(array_unique($ranks)); yields
Array ( [0] => Maj [1] => MSgt [2] => Capt [6] => SSgt )

The key values shown represent the key value where the element first appeared.


Merges the elements of two or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
$newbies = array("Johnson"=>"TSgt", "Williams"=>"SSgt");
print_r(array_merge($squadron,$newbies)); yields
  Array ( [Smith] => MSgt [Jones] => SSgt [Williams] => SSgt [Adams] => LtCol [Johnson] => TSgt )

Since 'Williams' was in both arrays, the latter value controls.


Returns an array containing all the values of $arr1 that are present in all the arguments. Note that keys are preserved.
$officer = array("Gen","LtGen","MGen","BGen","Col","LtCol","Maj","Capt","1Lt","2Lt");
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_intersect($squadron,$officer)); yields
Array ( [Williams] => Capt [Adams] => LtCol )


Searches the array for a given value and returns the corresponding key if successful.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_search('LtCol',$squadron)); yields Adams


iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.
$officer = array("Gen","LtGen","MGen","BGen","Col","LtCol","Maj","Capt","1Lt","2Lt");
function checkMember($arg) {global $officer; return in_array($arg,$officer);}
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
print_r(array_filter($squadron,"checkMember")); yields
Array ( [Williams] => Capt [Adams] => LtCol )

foreach($array as $key => $value)

Iterates through the array ready to do whatever you want with the keys and values.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");

  foreach ($squadron as $key => $value) {echo "Key: $key; Value: $value<br>\n";}
 Key: Smith; Value: MSgt
 Key: Jones; Value: SSgt
 Key: Williams; Value: Capt
 Key: Adams; Value: LtCol


It takes an associative array $assocArray and treats keys as variable names and values as variable values.
$squadron = array("Smith"=>"MSgt", "Jones"=>"SSgt", "Williams"=>"Capt", "Adams"=>"LtCol");
echo "Smith = ".$Smith.", Jones = ".$Jones.", Williams = ".$Williams.", Adams = ".$Adams; produces
Smith = MSgt, Jones = SSgt, Williams = Capt, Adams = LtCol

Strings   < <  PREVIOUS   Table of Contents NEXT  > >   Conditionals

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 341 different visits in the last 30 days.
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 2:07 am, UTC
This file took 0.00521 seconds to process.