PHP Array Functions

In this lesson, you will learn about the Array function in PHP, and its usage, along with examples to better understand the topic.


PHP Array Functions

PHP offers several array functions that can be used to access and modify an array’s elements. Use the is_array() function to check whether a variable is an array. Now that array functions have been implemented, let’s look at an example.

Basic Syntax

array arrayName ([ any type of values $... ] )

Example

<?php
echo "PHP ***isarray built-in function*** Example";
echo "<br>";
echo " ------------is_array ------------------ ";
echo "<br>";
$luxuryCars = array(
    "luxuryCar1Name" => "Volvo",
    "luxuryCar2Name" => "Audi",
    "luxuryCar3Name" => "Ferrari"
);
echo is_array($luxuryCars);
?>

Output

PHP ***isarray built-in function*** Example
------------is_array ------------------
1//true it is an array

PHP sort function

The sort() function is used for the value sorting of arrays. If the values are alphanumeric, the sort() function will sort them alphabetically. If the values are numeric, it will sort the array in ascending order. The existing access keys are removed, and new numeric keys are added. This function’s output is a number array.

Example

<?php
echo "PHP ***Sort built-in function*** Example";
echo "<br>";
echo " ------------sort ------------------ ";
echo "<br>";
$luxuryCars = array(
    "luxuryCar1Name" => "Volvo",
    "luxuryCar2Name" => "Audi",
    "luxuryCar3Name" => "Ferrari"
);
sort($luxuryCars);
print_r($luxuryCars);
?>

Output

PHP ***Sort built-in function*** Example
------------sort ------------------
Array ( [0] => Audi [1] => Ferrari [2] => Volvo )

PHP ksort() Function

You can use the ksort() function to sort arrays by the keys’ alphabetical order. The instance below shows its use.

Example

<?php
echo "PHP ***Sort built-in function*** Example";
echo "<br>";
echo "
------------ksort ------------------ ";
echo "<br>";
$nameoffruits = array(
    " fruits1" => "Apple",
    " fruits2" => "Banana",
    " fruits3" => "Orange"
);
ksort($nameoffruits);
print_r($nameoffruits);
?>

Output

PHP ***Sort built-in function*** Example
------------ksort ------------------
Array ( [ fruits1] => Apple [ fruits2] => Banana [ fruits3] => Orange )

PHP asort() function

asort function: use the asort() function to sort an array without changing its associative indices. The instance below shows its use.

Example

<?php
echo "PHP ***Sort built-in function*** Example";
echo "<br>";
echo " ------------asort ------------------ ";
echo "<br>";
$nameoffruits = array(
    " fruits1" => "Apple",
    " fruits2" => "Mango",
    " fruits3" => "Orange",
    " fruits4" => "Banana"
);
asort($nameoffruits);
print_r($nameoffruits);
?>

Output

PHP ***Sort built-in function*** Example
------------asort ------------------
Array ( [ fruits1] => Apple [ fruits4] => Banana [ fruits2] => Mango [ fruits3] => Orange )

In the example mentioned above, the banana was allocated at the 4th index, but after sorting using assort, it displayed on the 2nd number as B comes before O and M.

This concludes the PHP Array Functions lesson. In the next class, you will learn about the associative array in PHP.