PHP 8 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 a 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 a comma.

Example

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

The key is optional in the array, we can also enter only values as an argument in the 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 arrays as values, then it is called a multidimensional array.

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

Indexed Array

In an indexed array, keys are in sequential form or optional. We can store numbers, strings and any object in an indexed array. There is an index value represented by a number.

Example

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




Associative Array

The associative arrays are basically the same as numeric arrays in term of functionality but they are distinctive in terms of their index value. In this, elements are defined in key value pairs. Associative array will have their key as a string, so you can set up a solid relationship among key and values.

Example

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

Multidimensional Array

In a multidimensional array, array element contains one or more array. It is also known as array of arrays. In this, we can store another array at each index rather than storing a single value.

Example

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

Retrieving Array value

To direct retrieve an array value, we use its index. Suppose we have stored a value in the $arr array at index 4, $arr[4] should return the stored value. If nothing has been stored at the 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 the 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 eliminates the previous keys and reassigns the 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