| Recommend this page to a friend! |
| Info | Documentation | Reputation | Support forum | Blog | Links |
| Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
| 2024-12-27 (6 months ago) | Not enough user ratings | Total: 6 | All time: 11,529 This week: 31 | |||||
| Version | License | PHP version | Categories | |||
| array-functions 1.0.0 | MIT/X Consortium ... | 8.0 | Language, PHP 8, Global |
| Description | Author | |
This package provides PHP array functions that work in older versions. |
A lightweight utility package for array operations offering procedural functions like array_find, array_all, and more. These functions are already supported in PHP 8.1 and later. This package provide backward compatibility for PHP 8.0.
Recommend installation method:
composer require luminovang/array-functions
You can also use the function in another projects.
include_once __DIR__ . '/vendor/luminovang/array-functions/src/ArrayFuncs.php;';
You can import multiple functions at once using the use function syntax with braces around the function names:
use function Luminova\Procedural\ArrayFunctions\{
array_find,
array_find_key,
array_any,
array_all
};
Importing a Specific Function:
To import a specific function, such as array_find, use the following syntax:
use function Luminova\Procedural\ArrayFunctions\array_find;
The array_find function allows you to find the first element in an array that satisfies a given condition specified in a callback.
$result = array_find([1, 2, 3, 4, 5], fn(int $value) => $value > 3);
echo $result; // Output: 4
> In this example, array_find returns the first element greater than 3, which is 4.
The array_find_key function searches for the first key where the corresponding value meets the given condition.
$result = array_find_key(['apple', 'banana', 'cherry'], fn(string $value) => $value === 'banana');
echo $result; // Output: 1
> Here, array_find_key finds the key of 'banana', which is 1.
Another Example
Find key using str_starts_with.
$result = array_find_key(
['java' => 1, 'php' => 2, 'swift' => 3],
fn(int $value, string $key) => str_starts_with($key, 'p')
);
echo $result; // Output: php
> In this case, array_find_key returns the key 'php', where the key starts with 'p'.
The array_all function checks if all elements in the array satisfy the condition defined in the callback.
$result = array_all([2, 4, 6], fn(int $value) => $value % 2 === 0);
echo $result; // Output: true
> In this example, array_all returns true because all elements in the array are even numbers.
The array_any function checks if at least one element in the array meets the condition specified in the callback.
$result = array_any([1, 2, 3], fn(int $value) => $value > 2);
echo $result; // Output: true
> In this case, array_any returns true because one element (3) is greater than 2.
| File | Role | Description | ||
|---|---|---|---|---|
| Data | Auxiliary data | |||
| Lic. | License text | |||
| Data | Auxiliary data | |||
| Doc. | Documentation | |||
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% |
|
|
| Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.