Login

PHP Array Function Tutorial

PHP array string functions are some of the most important functions you need to know in PHP if you want to become an efficient programmer. In this tutorial, we will look at the most commonly used array string functions, including (but not limited to) count, push, in_array, asort, and pop.

Quick Guide to Array Basics

PHP arrays are a simple way of storing numerous bits of information (such as strings) in a simple PHP variable, known as an “array variable”; you can retrieve this information by using its key index.

On the other hand, “strings” are a finite series of symbols from a set such as an alphabet. In PHP, you can designate a string with single or double quotes:

<?php
$string1= 'This is a string created by single quote';
$string2= "This is a string created by double quotes";
?>

Most of the information you gather in a PHP web form is in string data format. Storing strings in an array allows your code to use only a single PHP variable to store a lot of information. Supposing you would like to define an array variable that consist of the planets in the solar system, you would do so in the following manner:

<?php
//create an array variable $planets
$planets=array();
//store strings in the $planet array variable, which is essentially the name of the planets
$planets=array("Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune");
//dump all string information stored in the array variable to the browser
var_dump($planets);
?>

The numbers [0] to [7] are the key index values corresponding to each piece of string information stored in the array. You can retrieve string information by calling its key. Supposing you will need to retrieve the planet “Jupiter”, the index key is 4:

<?php
var_dump($planets[4]);
?>

<?php
var_dump($planets[4]);
?>

There are also “associative arrays”, which are useful if you do not use a sequential numerical key index for your string elements. For example:

<?php
//Example1. Associative array of a person and its favorite pet
$favoritepet=array("Mary"=>"Dog","John" =>"Snakes","Paul"=>"Centipede","Peter"=>"Cat");
var_dump($favoritepet);
//Example2. Associative array of a person and its weight in kilos
$weight=array("Mary"=>50,"John" =>60,"Paul"=>80,"Peter"=>71);
var_dump($weight);
?>

Another way to add elements in the array is by equating the string to be inserted with the array variable:

$arrayvariable[]="String to be inserted to the array"

Illustration: Add the five letters to an existing array of alphabetical letters.

<?php
$fiveletters=array("d","e","f","e","f");
$existingarray=array("a","b","c");
//Read the five letters array by looping on it and then adding it to the existing array
foreach ($fiveletters as $value) {
$existingarray[]=$value;
}
//dump the updated elements in $existing array
var_dump($existingarray);
?>

3.) in_array() = check if a specific string is present in an array. Example: Supposing you will check if the name “Linda” is present in the $nameofgirl array.

<?php
$nameofgirl=array();
$nameofgirl= array("Amanda","Jenna","Elizabeth");
array_push($nameofgirl, "Linda", "Jenny","Anna");
//check if name "Linda" is present in the array
if (in_array("Linda",$nameofgirl)) {
echo "Yes the name Linda is found on the array";
} else {
echo "Sorry but the name Linda is not on the array";
}
?>

The above code will return “Yes the name Linda is found in the array”.

4.) array_unique() = removes duplicate values on the array. Example:

Supposing you have an array of colors:

<?php
//Supposing you have an array of colors, red is a duplicate
$fivecolors=array("blue","red","yellow","red","magenta");
var_dump($fivecolors);
?>

Below is the dump of this array (take note of the index key value of each string element):

5.) array_values() = this will be used to reset any gaps in the indexes caused after implementation of array_unique. In the previous example, you will notice that the indexes are 0,1,2 and 4 (3 is removed because it is a duplicate). You can reset it back to 0,1,2,3,4 using array_values() function:

6.) array_change_key_case() = this function can be used to produce a uniform casing in all strings present in the array. Sometimes you will be processing string elements in an array that does not have uniform casing distribution (for example, strings with both upper and lowercases). If you need to make all casing “upper case” or “lower case”; then you can use this function.

The result is False/Null or 0 logic because white color is not included in the array. Bear in mind that 0 can be a result of two possible scenarios; the first one is that it did exist in the array and found on the first element. The second scenario is when array_search failed to find the string in the array, which in some logic can be evaluated to 0/False or null. To avoid this type of problem, you might need to test first if the string really did exist in the array by using an in_array() function.

<?php
$fivecolors=array("blue","red","yellow","red","magenta");
//Search the color white and return the key
//Check first if white color is indeed present in the array
if (in_array("white",$fivecolors)) {
//white is present in the array, now extract the key
$key = array_search('white', $fivecolors);
echo $key;
} else {
//white is not present in the array.
echo "The key does not exist for this color";
}
?>

9.) asort = this will sort the string elements found in the array variable alphabetically and maintain the corresponding index.