The document discusses various PHP array functions such as sizeof(), is_array(), print_r(), array_merge(), array_values(), array_keys(), array_pop(), array_push(), array_shift(), sort(), array_map(), array_walk(), and array_flip(). It provides examples of how each function can be used to access, manipulate, and operate on array elements in PHP.
2. PHP provides various array functions to access and
manipulate the elements of array.
rray function will save you a lot of time as they are
pre-defined in PHP libraries and all you have to do is
call them to use them.
3. This function returns the size of the array or the
number of data elements stored in the array.
It is just like count($arr) method.
sizeof($arr)
4. <?php
$lamborghinis = array("Urus", "Huracan",
"Aventador");
echo "Size of the array is: ". sizeof($lamborghinis);
?>
Output:
Size of the array is: 3
5. To check whether the provided data is in form of an array,
we can use the is_array() function.
<?php
$lamborghinis = array("Urus", "Huracan", "Aventador"); //
using ternary operator
echo is_array($lamborghinis) ? 'Array' : 'not an Array';
$mycar = "Urus"; // using ternary operator
echo is_array($mycar) ? 'Array' : 'not an Array';
?>
Output:
Array
not an Array
is_array($arr)
6. Although this is not an array function, but it deserves
a special mention here, as we can use this function to
print the array in the most descriptive way possible.
This function prints the complete representation of
the array, along with all the keys and values.
print_r($arr)
8. If you want to combine two different arrays into a
single array, you can do so using this function.
It doesn't matter whether the arrays to be combined
are of same type(indexed, associative etc)
or different types, using this function we can combine
them into one single array.
array_merge($arr1, $arr2)
9. <?php
$hatchbacks = array( "Suzuki" => "Baleno",
"Skoda" => "Fabia",
"Hyundai" => "i20",
"Tata" => "Tigor" ); // friends who own the above cars
$friends = array("Vinod", "Javed", "Navjot",
"Samuel"); // let's merge the two arrays into one
$merged = array_merge($hatchbacks, $friends);
print_r($merged);
?>
11. In an array, data is stored in form of key-value pairs,
where key can be numerical(in case of indexed array)
or user-defined strings(in case of associative array)
and values.
If we want to take all the values from our array,
without the keys, and store them in a separate array,
then we can use array_values() function.
array_values($arr)
12. <?php
$hatchbacks = array(
"Suzuki" => "Baleno",
"Skoda" => "Fabia",
"Hyundai" => "i20",
"Tata" => "Tigor"
); // friends who own the above cars
$friends = array("Vinod", "Javed", "Navjot", "Samuel"); //
let's merge the two arrays into one
$merged = array_merge($hatchbacks, $friends); //getting
only the values
$merged = array_values($merged);
print_r($merged);
?>
19. This function is the opposite of
the array_pop() function.
This can be used to add a new element at the end of
the array.
array_push($arr, $val)
20. <?php
$lamborghinis = array("Urus", "Huracan", "Aventador"); //
adding a new element at the end
array_push($lamborghinis, "Estoque");
print_r($lamborghinis);
?>
Output:
Array (
[0] => Urus
[1] => Huracan
[2] => Aventador
[3] => Estoque
)
21. This function can be used to remove/shift the first
element out of the array.
So, it is just like array_pop() function but different in
terms of the position of the element removed.
array_shift($arr)
22. <?php
$lamborghinis = array("Urus", "Huracan",
"Aventador"); // removing the first element
array_shift($lamborghinis);
print_r($lamborghinis);
?>
24. Similar to this, we have another
function array_unshift($arr, $val) to add a new
value($val) at the start of the array(as the first
element).
25. This function sorts the array elements in ascending
order. In case of a string value array, values are sorted
in ascending alphabetical order.
Some other sorting functions
are: asort(), arsort(), ksort(), krsort() and rsort().
sort($arr)
28. If you want to perform certain operation on all the values
stored in an array, you can do it by iterating over the array
using a for loop or foreach and performing the required
operation on all the values of the array.
Or, you can use the function array_map(). All we have to
do is define a separate function to which we will provide
the values stored in the array one by one(one at a time)
and it will perform the operation on the values. Let's have
an example,
array_map('function_name', $arr)
29. <?php
function addOne($val)
{ // adding 1 to input value
return ($val + 1);
}
$numbers = array(10, 20, 30, 40, 50); // using
array_map to operate on all the values stored in array
$numbers = array_map('addOne', $numbers);
print_r($numbers)
?>