PHP 7 Arrays

An array is a collection of key/value pairs. We can store more than one item in only one variable i.e. Array. So Array is used when there is requirement to add more items in a single variable. To create an array, we are using an array () construct. Each element in an array is separated by comma.

Example

<?php
    array(key1=>value1, key2=>value2, key3=>value3);
?>

The key is optional in the array, we can also enter only values as argument in array, like-
array(value1, value2, value3);

Array indexes can be integer and string. Array values can be of any data types, it can also be an array. If an array contains one or more array as values than it is called multidimensional array.

Types of Arrays in PHP- Indexed Array, Associative Array and Multidimensional Array

Indexed Array

In indexed array, keys are in sequential form or optional.

Example

<?php
    $array1 = array('Book', 'Table', 'Chair'); 
?>
OR
<?php
    $array1 = array();
    $array1[0] = 'Book';
    $array1[1] = 'Table';
    $array1[2] = 'Chair';
?>




Associative Array

In Associative array, elements are defined in key/value pairs.

Example

<?php
    $arr = array('k1'=>'Book', 'k2'=>'Table', 'k3'=>'Chair' );
?>

Multidimensional Array

In a multidimensional array, array element contains one or more array.

Example

<?php
    $arr = array('k1'=>array('Book', 'Pen', 'Copy'), 
				'k2'=> array('Table', 'Chair'));
?>

Retrieving Array value

To direct retrieve array value, we use its index. Suppose we have stored a value in $arr array at index 4, $arr[4] should return the stored value. If nothing has been stored at index of 4, $arr[4] will behave like an unbound variable.

Example

<?php
    $arr = array('Book', 'Pen', 'Copy', 'Table', 'Chair');
	echo $arr[4];
?>

Output

Chair




Print Arrays

PHP provide two functions to print composite data types - print_r and var_dump. Both functions print out the contents of array. var_dump also outputs the data types of each value.

Example

<?php
    $arr = array('k1'=>array('Book', 'Pen', 'Copy'), 
				'k2'=> array('Table', 'Chair'));
	print_r($arr);
	var_dump($arr);
?>

Output

Array
(
    [k1] => Array
        (
            [0] => Book
            [1] => Pen
            [2] => Copy
        )

    [k2] => Array
        (
            [0] => Table
            [1] => Chair
        )

)

array(2) { ["k1"]=> array(3) { [0]=> string(4) "Book" [1]=> string(3) "Pen" [2]=> string(4) "Copy" } ["k2"]=> array(2) { [0]=> string(5) "Table" [1]=> string(5) "Chair" } }




Deleting from Arrays

Deleting an element from an array is simple, exactly equivalent to getting rid of an alloted variable. unset() function is used to delete items from an array.

Example

<?php
    $arr = array('Book', 'Pen', 'Copy', 'Table', 'Chair');
	unset($arr[4]);
	print_r($arr);
?>

Output

Array
(
    [0] => Book
    [1] => Pen
    [3] => Table
    [4] => Chair
)

Useful PHP Array functions

array_reverse() This function reverses the order of elements in an array.
array_values() This function returns all the values from an array and auto eliminate the previous keys and reassign index starting from 0.
array_diff() This function is used to get array differences. This function returns those values from the first array that are not present in other arrays.
array_key_exists() This function is used to check the existence of the given array index in the array.
array_map() This function is used to apply a callback function on each element of an array.
array_push() This function is used to push one or more elements to the end of an array.
array_search() This function searches a value in an array and returns its key.
array_unique() This function removes the duplicate values from the array.
count() This function returns total number of elements in an array.
each() This is used to return the current key value pair of an array.
end() This function is used to return the last element of an array.
in_array() This function is used to check the existence of a value in an array or not.


Practice Exercises






Read more articles


General Knowledge



Learn Popular Language