In any programming language, an array is a variable that stores multiple values. Therefore, the PHP array is no exception. In this tutorial, we are going to discuss what is an array, how to create a PHP array, how to access elements of an associative array, a multidimensional array, and how to add and remove PHP array elements.
If you have more than one value, you can store them in an array variable. For example, consider the following values:
$car1 = "Ferrari";
$car2 = "Benz";
$car3 = "BMW";
$car4 = "Volvo";
As you can see, these four variables increase the number of coding lines and decrease the readability of the code. Also, if you want to search for the name of a car, you will face a problem. To avoid this, we use an array.
Advanced: A PHP array is actually a sorted map. the map is a variable that associates values with keys. This type of variable has several uses. It can be thought of as an array, list, hash table, dictionary, collection, stack, queue, and more. Since array values can be other arrays, trees, and multidimensional arrays are also possible.
PHP array can act as both list and map.
Declaring a PHP array
In PHP, two different methods are used to define and create an array. One is array()
to define key-value arrays and normal arrays and []
to create an array where all values are placed inside brackets.
Array values can be of any data type.
$cars = array("Ferrari","Benz","BMW","Volvo",100,true);// or
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo",
"han" => 100,
"bool" => true
);// or
$cars = ["Ferrari","Benz","BMW","Volvo",100,true];
The number of array values is unlimited. One thing to keep in mind when defining key-value arrays is that array keys must be unique. If you have two keys with the same name in the array, PHP will consider the last key valid. We will discuss this issue further in this article.
If you are not sure about the values of the array and you will initial the array later, you can use the following code.
$cars = array();
$cars[] = "Ferrari";
$cars[] = "Benz";
$cars[3] = "BMW";
$cars['volvo'] = "Volvo";var_dump($cars);
------------------
array (size=4)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
3 => string 'BMW' (length=3)
'volvo' => string 'Volvo' (length=5)
Note: If you put a comma after the last value, it won’t cause any problem like below
$cars = array("Ferrari","Benz","BMW","Volvo",);
This method is actually created to define arrays in several lines.
$cars = array(
"ferrari" => "Ferrari",
"benz" => "Benz",
"bmw" => "BMW",
"volvo" => "Volvo",
);
If the array already exists and you want to change its previous value, you can use the following code.
$cars[1] = "Toyota";echo $cars[1];
-----------------
Toyota
You can also use the array_combine()
function to create key-value arrays.
$keys = array("fer","ben","bmw","vol");
$cars = array("Ferrari","Benz","BMW","Volvo");$arr = array_combine($keys,$cars);var_dump($arr);
-----------------
array (size=4)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
'vol' => string 'Volvo' (length=5)
Types of arrays in PHP
There is three PHP array:
- Indexed array: This type of array uses numbers to access the items of the array.
- Associative array: This type uses keys to access the items.
- Multidimensional array: In this type of array, you can store an array inside another array.
1- Indexed Array
This array is defined above. For this array, just put the values in the array.
$cars = ["Ferrari","Benz","BMW","Volvo"];
echo $cars[1];
----------------
Benz
2- Associative Array
This type of array uses keys instead of index numbers. The key can be a number or a string and the value can be of any data type.
Key names follow the following rules:
- Strings containing decimal numbers can be introduced as keys. If the number is preceded by a + sign, it will be converted to a number. for example. “8” key is allowed. While “08” is not allowed, because it is not a valid decimal integer.
- Float numbers are converted to integers. in such a way that their float part is removed.
- A null value will be converted to an empty string (“”).
- Arrays and objects cannot be used as keys. Otherwise, an error warning will be displayed.
$cars = array(
11 => "Ferrari",
"12" => "Benz",
13.5 => "BMW",
true => "Volvo",
);
var_dump($cars);
------------------
array (size=4)
11 => string 'Ferrari' (length=7)
12 => string 'Benz' (length=4)
13 => string 'BMW' (length=3)
1 => string 'Volvo' (length=5)
To get a value from the array, we must use ” even if our key is numerical (not index).
echo $cars['11'];
--------------------
Ferrari
Note: If the same key is used several times, the last key is valid.
$cars = array(
1 => "Ferrari",
"1" => "Benz",
1.5 => "BMW",
true => "Volvo",
);
var_dump($cars);
--------------------
array (size=1)
۱ => string 'Volvo' (length=5)
Because all the keys have been changed to 1, so only the last key is valid.
3- Multidimensional Array
In a multidimensional array, we can put an array in another array. This section is about how to create a multidimensional array and how to access elements of a multidimensional array in PHP.
$cars = array(array("Ferrari","Benz","BMW","Volvo"),array("Toyota","Audi"));var_dump($cars);
-----------------------
array (size=2)
0 =>
array (size=4)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
1 =>
array (size=2)
0 => string 'Toyota' (length=6)
1 => string 'Audi' (length=4)
To get the value of an item from the array, we use the following method.
echo $cars[1][1];
---------------------
Audi
To change the value, we use the same as the one-dimensional array, but with a little change.
$cars[1][1] = "Ford";echo $cars[1][1];
-------------------
Ford
Accessing elements of an Array
We use numbers to get the values of the Indexed array.
echo $cars[1]; ===> Benz
echo "Cars: " . $cars[0] . "," . $cars[1] . "," . $cars[2] . "," . $cars[3];
-----------------
Cars: Ferrari,Benz,BMW,Volvo
As you can see, the output is “Benz”.
In PHP, arrays start at index zero. That is, the first item of the array is zero, not one.
Note: In PHP 8, we can use {}
instead of []
.
echo $cars{1};
----------------
Benz
Other ways to get the value
function getArray() {
return array("Ferrari","Benz","BMW","Volvo");
}
$secondElement = getArray()[1];
// or
list(, $secondElement) = getArray();echo $secondElement;
---------------------
Benz
Using the array_values()
function
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);var_dump(array_values($cars));
----------------------
array (size=4)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
Using the array_pop()
function, you can also extract the last value of the array, but with the difference that this function removes the last value from the array.
$cars = array("Ferrari", "Benz", "BMW", "Volvo");var_dump(array_pop($cars));var_dump($cars);
-----------------------------
Volvoarray (size=3)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
Another function that you can use to extract the first value of the array is the array_shift()
function. This function returns the value of the first item and removes the item from the array.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);$value = array_shift($cars);var_dump($value);
var_dump($cars);
-----------------
Ferrariarray (size=3)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
'vol' => string 'Volvo' (length=5)
Adding elements in a PHP array
There are different ways to add a new value to the array.
$cars = array("Ferrari","Benz","BMW","Volvo");
$cars[] = "Toyota";var_dump($cars);
--------------------
array (size=5)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
4 => string 'Toyota' (length=6)
Adds an item with index 4 to the end of the array and sets its value to “Toyota”.
$cars = array("Ferrari","Benz","BMW","Volvo");
$cars['a'] = "Toyota";var_dump($cars);
-------------------
array (size=5)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
'a' => string 'Toyota' (length=6)
In PHP 7.4.0 and later, it is possible to add a value to the beginning and end of the array with “…”. See Expand the array
The array_push()
function can also be used to add a value to the end of the array.
$cars = array("Ferrari","Benz","BMW","Volvo");array_push($cars,"Toyota","Audi");var_dump($cars);
-------------------
array (size=6)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
4 => string 'Toyota' (length=6)
5 => string 'Audi' (length=4)
This method is only used for Indexed arrays and keys cannot be defined for values.
Using the array_unshift()
function, a value can be added to the beginning of the array.
$cars = array("Ferrari", "Benz", "BMW", "Volvo");array_unshift($cars, "Toyota");var_dump($cars);
--------------------
array (size=5)
0 => string 'Toyota' (length=6)
1 => string 'Ferrari' (length=7)
2 => string 'Benz' (length=4)
3 => string 'BMW' (length=3)
4 => string 'Volvo' (length=5)
Read More: PHP Array Add Element
Remove an element from an array
There are also different methods to remove an item from the array.
The first method is to use the unset()
function.
$cars = array("Ferrari","Benz","BMW","Volvo");
unset($cars[2]);var_dump($cars);
-----------------
array (size=3)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
3 => string 'Volvo' (length=5)
The value “BMW” was removed from the array.
Note: This function removes the value from the array, but does not modify its index. In the previous example, there is no index 2. To modify the index, we can use the array_values()
function.
$cars = array_values($cars);
var_dump($cars);
------------------
array (size=3)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'Volvo' (length=5)
The length of the array
To get the number of items in the PHP array, we use the count()
function.
$cars = array("Ferrari","Benz","BMW","Volvo");
echo count($cars);
------------------
4
The output value is 4. Because the number of car names is 4.
More details: PHP Array Length
Duplicate a PHP array
Duplication of PHP arrays is very simple; Just set the array equal to the array.
$cars1 = array("Ferrari","Benz","BMW","Volvo");$cars2 = $cars1;var_dump($cars2);
--------------------
array (size=4)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
Note: This method has a small problem that you should pay attention to. Arrays are stored in memory, and in this method, the array is stored in two memory locations, if the array is large, the amount it will take from the memory is significant. If you don’t need the first array, you can reproduce it by reference so that the memory is occupied only once.
$cars2 = &$cars1;
Remember that if you change the values of the first array, the second array will also change.
Concatenation and merge arrays
The array_merge()
function is used to merge arrays in PHP.
$cars1 = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);
$cars2 = array(
"toy" => "Toyota",
"aud" => "Audi"
);$cars = array_merge($cars1, $cars2);var_dump($cars);
--------------------
array (size=6)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
'vol' => string 'Volvo' (length=5)
'toy' => string 'Toyota' (length=6)
'aud' => string 'Audi' (length=4)
The array_push()
function can also be used to append arrays.
$cars1 = array("Ferrari", "Benz", "BMW", "Volvo");
$cars2 = array("Toyota", "Audi");array_push($cars1, $cars2);var_dump($cars1);
--------------------
array (size=5)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
4 =>
array (size=2)
0 => string 'Toyota' (length=6)
1 => string 'Audi' (length=4)
The difference is that this function converts the array into a multidimensional array.
More details: PHP Array Merge
Looping through PHP array elements
The array can store multiple values, it can be easily used in iteration loops like foreach
.
$cars = array("Ferrari","Benz","BMW","Volvo");foreach($cars as $car){
echo $car."-";
}
-----------------
Ferrari-Benz-BMW-Volvo-
Keys can also be used in loops.
foreach ($cars as $key => $car) {
echo $key . ":" . $car . "-";
}
------------------
fer:Ferrari-ben:Benz-bmw:BMW-vol:Volvo-
Array values can be changed by referencing them in iteration loops.
$cars = array("Ferrari","Benz","BMW","Volvo");foreach($cars as &$car){
$car = $car . "1";
}var_dump($cars);
--------------------
array (size=4)
0 => string 'Ferrari1' (length=8)
1 => string 'Benz1' (length=5)
2 => string 'BMW1' (length=4)
3 => string 'Volvo1' (length=6)
Note: Pay attention to the & before the $cars variable name in the loop.
Sorting array in PHP
Arrays can be easily sorted by sort()
function. Returns true if the sorting is successful, otherwise false. By default, this function sorts the array in ascending order.
$cars = array("Ferrari","Benz","BMW","Volvo");sort($cars);var_dump($cars);
--------------------
array (size=4)
0 => string 'BMW' (length=3)
1 => string 'Benz' (length=4)
2 => string 'Ferrari' (length=7)
3 => string 'Volvo' (length=5)
Along with the sort()
function are other functions that do the same sort as the function but with a bit more control. Although the sort()
function is the main function, if it is accompanied by some special characters, it will behave differently, which we will discuss below.
- a: Sorting with key retention
- k: Sort by key
- r: Descending sort
- u: Sort by user function
Here are the functions
- asort
- arsort
- uasort
- ksort
- krsort
- arsort
- krsort
- rsort
- uasort
- usort
function hs_custom_sort($value1, $value2)
{
return strcmp($value1, $value2);
}
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);usort($cars, 'hs_custom_sort');var_dump($cars);
-----------------
array (size=4)
0 => string 'BMW' (length=3)
1 => string 'Benz' (length=4)
2 => string 'Ferrari' (length=7)
3 => string 'Volvo' (length=5)
If your data contains characters other than English, PHP has a way to sort them. You can use PHP local for this.
setlocale(LC_ALL, 'sk_SK.utf8');$words = ["ďateľ", "auto", "železo", "byt", "kocka", "dáma", "zem", "autor", "ceduľa", "čižma"];sort($words, SORT_LOCALE_STRING);var_dump($words);
--------------------
array (size=10)
0 => string 'auto' (length=4)
1 => string 'autor' (length=5)
2 => string 'byt' (length=3)
3 => string 'ceduľa' (length=7)
4 => string 'dáma' (length=5)
5 => string 'kocka' (length=5)
6 => string 'zem' (length=3)
7 => string 'čižma' (length=7)
8 => string 'ďateľ' (length=7)
9 => string 'železo' (length=7)
First of all, we set the desired local language and then sort. In this example, the Slovak language is defined in this example.
More details: PHP Array Sort
Searching PHP array
We use the array_search()
and in_array()
functions to find the location of a value inside the array.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);if(in_array("Ferrari",$cars)){
echo "Exists in " . in_array("Ferrari",$cars);
}
else{
echo "Not Exists";
}
------------------
Exists in 0
Returns the index of the value. If there is no value in the array, it returns false.
echo array_search("Ferrari",$cars);
------------------
fer
Returns the key. If there is no value in the array, it returns false.
Functions are case-sensitive.
If you just want to check if the value exists in the array and you don’t need its value, you can use the array_key_exists()
function.
if(array_key_exists("Benz",$cars)){
echo "Exists";
}
More details: PHP Array Search
Expand the array
In PHP 7.4.0 we can open arrays using “…” like “…” in ReactJS. With this method, you can add a value to the beginning or end of the arrays or merge the arrays together.
$cars = array("Ferrari","Benz");
$arr1 = [...$cars]; //["Ferrari","Benz"]
$arr2 = ["Toyota",...$cars]; //["Toyota","Ferrari","Benz"]$cars2 = array("Toyota","Audi");
$arr = [...$cars,...$cars2,"Ford"]; //["Ferrari","Benz","Toyota","Audi","Ford"]
In merging two arrays together (last example), if the names of the keys in the two arrays are the same, the key of the second array will be valid.
Convert array to string in PHP
There are several ways to convert a PHP array to a string. Among these functions is the implode()
function.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);echo implode(", ", $cars);
--------------------
Ferrari, Benz, BMW, Volvo
More details: PHP Array to String Conversion
The array_map()
function can also be used for multidimensional arrays.
$cars = array(
array(
'BMW' => 'Germany'
),
array(
'Ferrari' => 'Italy'
),
array(
'Honda' => 'Japan'
)
);echo implode(", ", array_map(function ($entry) {
return ($entry[key($entry)]);
}, $cars));
----------------
Germany, Italy, Japan
More details: PHP Array Map
Convert array to JSON and vice versa
json_encode()
function is used to convert array to JSON and json_decode()
function is used to convert JSON to object.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);echo json_encode($cars);
--------------------
{"fer":"Ferrari","ben":"Benz","bmw":"BMW","vol":"Volvo"}var_dump(json_decode('{"fer":"Ferrari","ben":"Benz","bmw":"BMW","vol":"Volvo"}'));
----------------
object(stdClass)[1]
public 'fer' => string 'Ferrari' (length=7)
public 'ben' => string 'Benz' (length=4)
public 'bmw' => string 'BMW' (length=3)
public 'vol' => string 'Volvo' (length=5)
In the JSON conversion method, the array is not created, but an object is created.
PHP array functions
is_array function
This function is used to check whether the variable is an array or not.
$cars = array(
"Ferrari",
"Benz",
"BMW",
"Volvo"
);echo is_array($cars);
-------------
1
If the variable is an array, it returns 1.
array_keys function
array_keys()
function is used to get array keys.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);var_dump(array_keys($cars));
-------------------------
array (size=4)
0 => string 'fer' (length=3)
1 => string 'ben' (length=3)
2 => string 'bmw' (length=3)
3 => string 'vol' (length=3)
array_flips function
array_flips()
function is also used to exchange the keys with their values.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);var_dump(array_flip($cars));
---------------------
array (size=4)
'Ferrari' => string 'fer' (length=3)
'Benz' => string 'ben' (length=3)
'BMW' => string 'bmw' (length=3)
'Volvo' => string 'vol' (length=3)
list function
It is designed to assign variables in a short way.
$cars = array("Ferrari","Benz","BMW","Volvo");list($a, $b, $c, $d) = $cars;var_dump($a);
----------------
Ferrari
This function is fully compatible with preg_slit()
and explode()
functions.
$cars = "Ferrari,Benz,BMW,Volvo";list($a,,, $d) = explode(",",$cars);var_dump($d);
----------------
Volvo
extract function
With the extract()
function, you can associate a key-value array with variables. For each item of an array, a variable with the name of the key is created, and its value is also set to the same value.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);extract($cars);var_dump($ben);
-----------------
Benz
The function creates variables named “fer”, “ben”, “bmw” and “vol”.
Note: If you are working with data that you have received from the database, it is better not to use this function, or if you want, then use EXTR_IF_EXISTS and EXTR_PREFIX_ALL flags.
compact function
This function is just the opposite of the extract() function. Creates an array of variables.
$fer = "Ferrari";
$ben = "Benz";
$bmw = "BMW";
$vol = "Volvo";$cars = compact('fer','ben','bmw','vol');var_dump($cars);
-------------------
array (size=4)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
'vol' => string 'Volvo' (length=5)
The values inside the function are the same as the names of the variables above, which must be entered exactly, otherwise, they will give an error.
array_filter function
This function is used to filter the PHP array. The first value is the array we want to filter, and the second value is the function itself, which will perform the filtering action.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo",
"fer2" => "Ferrari"
);$arr = array_filter($cars,function($car){
return $car == "Ferrari";
});var_dump($arr);
------------------
array (size=2)
'fer' => string 'Ferrari' (length=7)
'fer2' => string 'Ferrari' (length=7)
Returns the values containing the “Ferrari” expression as an array. The return value of the function is either true or false.
This function does not only filter based on the value, but by sending the third parameter, you can perform filtering based on the key or both the key and the value. For this, you need to set the ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH flags.
You can also use the array_filter()
function to remove empty values.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "",
"vol" => "Volvo"
);$arr = array_filter($cars);var_dump($arr);
-----------------
array (size=3)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'vol' => string 'Volvo' (length=5)
More details: PHP Array Filter
array_unique function
With this function, only unique values can be extracted from the array.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo",
"fer2" => "Ferrari"
);$arr = array_unique($cars);var_dump($arr);
---------------
array (size=4)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
'vol' => string 'Volvo' (length=5)
The “Ferrari” value at the end of the array is omitted because this value was already in the array.
array_column function
This function is used to extract columns of multidimensional arrays. These arrays may be populated via database commands or may be via a file such as CSV.
$cars = array(
array(
"name" => "Ferrari",
"year" => 2022
),
array(
"name" => "Benz",
"year" => 2020
)
);$arr = array_column($cars, "name");var_dump($arr);
----------------
array (size=2)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
array_map function
In filtering the array by this function, you can apply the desired filter values to each value.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);$arr = array_map('strtolower',$cars);var_dump($arr);// or
$arr = array_map(function($car){
return strtolower($car);
},$cars);var_dump($arr);
--------------------
array (size=4)
'fer' => string 'ferrari' (length=7)
'ben' => string 'benz' (length=4)
'bmw' => string 'bmw' (length=3)
'vol' => string 'volvo' (length=5)
In the first example, the function must be predefined.
function st2low($str){
return strtolower($str);
}$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);$arr = array_map('st2low',$cars);var_dump($arr);
-----------------
array (size=4)
'fer' => string 'ferrari' (length=7)
'ben' => string 'benz' (length=4)
'bmw' => string 'bmw' (length=3)
'vol' => string 'volvo' (length=5)
array_walk function
There are flaws in the array_map()
function. Among other things, you can’t easily pass the key to the function. That’s why we use this function. This function is similar to the array_map()
function, but with a slight difference. In this function, the array is sent by reference to the function.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);array_walk($cars,function(&$car,$key){
$car = "$key is a key of $car";
});var_dump($cars);
-------------------
array (size=4)
'fer' => string 'fer is a key of Ferrari' (length=23)
'ben' => string 'ben is a key of Benz' (length=20)
'bmw' => string 'bmw is a key of BMW' (length=19)
'vol' => string 'vol is a key of Volvo' (length=21)
array_diff function
It is used to remove the values from the array.
$cars1 = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);
$cars2 = array(
"bmw" => "BMW",
"toy" => "Toyota"
);$cars = array_diff($cars1, $cars2);var_dump($cars);
-------------------
array (size=3)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'vol' => string 'Volvo' (length=5)
Because BMW was in both arrays, it was removed from the first array.
array_intersect function
Unlike the array_diff()
function, this function is used to find commonalities between arrays.
$cars1 = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);
$cars2 = array(
"bmw" => "BMW",
"toy" => "Toyota"
);$cars = array_intersect($cars1, $cars2);var_dump($cars);
-------------------
array (size=1)
'bmw' => string 'BMW' (length=3)
array_slice function
This function is used to extract part of the array.
$cars = array("Ferrari", "Benz", "BMW", "Volvo");$cars = array_slice($cars, 1, 2);var_dump($cars);
--------------------
array (size=2)
0 => string 'Benz' (length=4)
1 => string 'BMW' (length=3)
The first parameter is the array from which we want to extract, the second parameter is the index of the item from which we want the extraction to start, and the third parameter is the number of items we want to extract.
array_reverse function
This function is used to reverse the items of the PHP array.
$cars = array("Ferrari", "Benz", "BMW", "Volvo");$cars = array_reverse($cars);var_dump($cars);
---------------------
array (size=4)
0 => string 'Volvo' (length=5)
1 => string 'BMW' (length=3)
2 => string 'Benz' (length=4)
3 => string 'Ferrari' (length=7)
array_rand function
This function returns one of the array items, key and index randomly.
$cars = array(
"Ferrari",
"Benz",
"BMW",
"Volvo"
);echo array_rand($cars);
-----------------
2
Combination of array_filter and array_map functions
With this combination, the array can be cleaned and arranged. Empty items and extra spaces can be removed.
$cars = array("Ferrari", "Benz", " BMW", "Volvo", " ", "Toyota");$cars = array_filter(array_map("trim", $cars));var_dump($cars);
---------------------
array (size=5)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
5 => string 'Toyota' (length=6)
More: PHP Array