PHP Arrays

An Array is a PHP datatype used to store a group of data into a variable. Each array item is stored as a key and value pair. Arrays are classified as “indexed array” and “associative array” based on the key specification. The Indexed array has default index starts with ‘0’ and the associative array contains the user-defined key index. The array can also be classified as “one-dimensional array” and “multi-dimensional array” based on the level of the array index. In this tutorial, we are going to see some of the important array functions in PHP.

group-beads

PHP Array Functions

In this section, we are going to see some of the widely used PHP array functions. I explain the purpose of the array functions, the parameter it requires and the default values of the parameter. This section includes small and simple PHP code snippets to explain the usage of each array function. 

range(start, end)

PHP range() function is used get the array of elements in the range between the start and end parameters. The range can be a numeric or alphabet. The following code shows how to get the array of alphabets that exist between “e” and “i”.

$character_array = range('e','i'); //  returns array('e','f','g','h','i');

reset(array)

The reset() function is used to reset the position of the array pointer. It requires the array variable as its argument to move the array pointer to its original position.

$character_array = range('e','i'); //  returns array('e','f','g','h','i');
print next($character_array); // prints f
print next($character_array); // prints g
reset($character_array);
print current($character_array); // prints the first element 'e' after reset

array_slice(array, offset, length, preserve_keys)

It is used to get the sub-array from the given input array. It accepts the input array, starting position as offset and the length of the sub array. The length parameter is optional. If it is not specified, the length of the input array will be taken by default. The fourth argument is to preserve keys by avoiding the default index reordering behavior.

$color_array = array("Red","Blue","Green","Yellow","Brown"); 
sliced_array = array_slice($color_array,1,3);//  now the sliced array will be array("Blue","Green","Yellow")

shuffle()

This function has the array as an argument to change the position of its value in a random manner.

array_chunk()

array_chunk() is used to break an array into the specified length of chunks, this function is used.

$character_array = array('e','f','g','h','i');
$chunks = array_chunk($character_array,2);
//$chunks will be as follows
/*
Array (
[0] => Array (
[0] => e
[1] => f
)
[1] => Array (
[0] => g
[1] => h
)
[2] => Array (
[0] => i
)
)
*/

merge()

As per the name of this function, it is for joining one or more arrays into a single array. The code shows the example,

$color_array1 = array("Red","Blue","Green");
$color_array2 = array("Yellow","Brown");
// returns array("Red","Blue","Green","Yellow","Brown");
$color_array = array_merge($color_array1,$color_array2);

array_key_exists()

This function is used to check whether the given key exists in the array or not. This function takes a key element and the input array as arguments.

array_keys(), array_values()

These two functions will get the keys and values of an input array, respectively. The following code shows the usage method of these functions and the resultant array of keys and values.

$student_array = array("name"=>"Martin","mark"=>"90","grade"=>"A");
$key_array = array_keys($student_array) // returns array("name","mark","grade")
$value_array = array_values($student_array) // returns array("Martin","90","A")

array_push()/array_pop()/array_shift()/array_unshift()

array_push() is used to push some values into an array. Using this function, a new element will be added to the end of a given input array. The array_pop() is used to get the last element of the array. The array_shift() and the array_unshift() methods are as similar as array_push() array_pop(). But, the insert and retrieve operations are performed at the starting position of an array.

array_splice()

This function is used to remove set of elements from a given array like array_slice. And it can also replace the removed elements with the new set of elements, provided, the length of the removed set and new array both should be same.

$character_array =  array('e','f','g','h','i');
$number_array = array('1','2','3');
$changed_array = array_splice($character_array,1,3,$number_array); // will hold the element like: array('e','1','2','3','i');

extract()/compact()

The compact() is used to convert the variable into an array, whereas, the extract() is used to extract an array and convert the element of that array as variables. The example shows it as follows. The second and third argument of extract() is optional. It is used to add a prefix to the variable name.

$student_array = array("name"=>"Martin","mark"=>"90","grade"=>"A");
extract($student_array,EXTR_PREFIX_ALL,"student"); 
echo $student_name . "-" . $student_mark . "(" . $student_grade . ")"; // prints as Martin - 90(A)
$compact_array = compact("student_name","student_mark","student_grade"); // will form an array

This PHP code tutorial was published on April 15, 2013.

↑ Back to Top