Arrays in PHP are used very widely. There are many functions for working with arrays. Let's check some of them and how they are used in Laravel open-source projects.
Example 1. is_array()
and count()
One of the functions used often is checking if a variable is an array. In this example from Filament, first, it is checked if $state
is an array, and if it is, then counts all elements in an array.
public function getItemsCount(): int{ $state = $this->getState(); return is_array($state) ? count($state) : 0;}
Link to the example in GitHub repository.
Example 2. array_merge()
and array_filter()
A common situation is to merge arrays into one. Another situation is to...