Array Operations in PHP

Let’s take a look at some of the most common built-in functions that we can use when working with arrays.

is_array function

The is_array() function checks if the variable is an array, and if it is, it returns true, otherwise it returns false. For example:

$users = ["Tom", "Bob", "Sam"];
$isArray = is_array($users);
echo ($isArray==true)?"this is an array":"this is not an array";

count/sizeof functions

The count() and sizeof() functions get the number of array elements:

$users = ["Tom", "Bob", "Sam"];
$number = count($users);
// same as
// $number = sizeof($users);
echo "In array users $number element/s";

shuffle functions

The shuffle function shuffles array elements randomly:

$users = ["Tom", "Bob", "Sam", "Alice"];
// one of the options
//Array ( [0] => Bob [1] => Tom [2] => Alice [3] => Sam )

compact functions

The compact function allows you to create an associative array from a set of variables, where the names of the variables will be the keys:


$model = "Apple II";
$producer = "Apple";
$year = 1978;

$data = compact("model", "producer", "year");
// get the following output
// Array ( [model] => Apple II [producer] => Apple [year] => 1978 )

The compact function receives a set of variables in parentheses. Each variable is quoted without the $ sign. The result of the function is a new array.

Sorting arrays

There are two types of sorting in PHP: sorting strings alphabetically and sorting numbers in ascending/descending order. If the values ​​to be sorted are strings, then they are sorted alphabetically; if they are numbers, then they are sorted in ascending numerical order. PHP chooses sorting type by default.

To sort in ascending order, use the asort function :

$users = ["Tom", "Bob", "Sam", "Alice"];
// output sorted array
// Array ( [3] => Alice [1] => Bob [2] => Sam [0] => Tom )

In this case, the array values ​​represent strings, so PHP will choose to sort alphabetically. However, with the help of an additional parameter, we can explicitly tell the PHP interpreter the sort type. This parameter can take three values:

  • SORT_REGULAR: automatic sort selection
  • SORT_NUMERIC: numeric sort
  • SORT_STRING: sort alphabetically

Let’s explicitly specify the sort type:

asort($users, SORT_STRING);

To sort an array in reverse order, use the arsort function :


Key sorting

The function asortsorts by element values, but there is also sorting by keys. It is represented by the ksort function :

$states = ["Spain" => "Madrid", "France" => "Paris", "Germany" => "Berlin", ];
// array after asort - sort by element values
// Array ( [Germany] => Berlin [Spain] => Madrid [France] => Paris )

// array after ksort - sort by element keys
// Array ( [France] => Paris [Germany] => Berlin [Spain] => Madrid )

Sorting by keys in reverse order is done by the krsort() function :


natural sort

Although the above sorting functions do their job well, they are still not enough. For example, let’s sort the following array in ascending order:

$os = array("Windows 7", "Windows 8", "Windows 10");
// result
// Array ( [2] => Windows 10 [0] => Windows 7 [1] => Windows 8 )

Since the values ​​represent strings, PHP sorts alphabetically. However, this sort does not respect numbers and case. Therefore, the value “Windows 10” will go at the very beginning, and not at the end, as it should have been. And to solve this problem, PHP has the natsort() function, which performs a natural sort:

$os = array("Windows 7", "Windows 8", "Windows 10");
// result
// Array ( [0] => Windows 7 [1] => Windows 8 [2] => Windows 10)

If we also want the sort to be case-insensitive, we can use the natcasesort() function :