November 25, 2015
Hot Topics:

Arrays in PHP

  • February 20, 2002
  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »

Array Functions

PHP includes a range of array-specific functions. These functions can help sort, manipulate, and search for information within arrays. The PHP manual contains a complete list of all the array functions, but let's take a look at a few of the more useful ones now:


This function works much like a mini case-insensitive search engine. With it, you can easily see if a value is contained within an array:


$Array=array("http://www.yahoo.com", "http://www.internet.com", "http://www.google.com", "http://www.cnn.com", "http://www.php.net");

if (in_array("yahoo")) {
   print "Hey! We found yahoo in this array";


This function, which determines how many elements are in an array, has already been briefly mentioned:


$Array=array("http://www.yahoo.com", "http://www.internet.com", "http://www.google.com", "http://www.cnn.com/", "http://www.php.net/");

$num = count($Array);
print $num;

As expected, this would return five.


One of several different functions to sort arrays, this particular one will sort the elements from lowest to highest (in the case of numerical values) or alphabetically (in the case of string values).


$Array=array("http://www.yahoo.com", "http://www.internet.com", "http://www.google.com", "http://www.cnn.com", "http://www.php.net");


for($i=0;$i<count($Array);$i++) {
print "$Array[$i]<br>";

Since we sorted this array before looping through it, the values will be printed out alphabetically, beginning with