PHP Classes

How to Use PHP Array Functions That Exist in PHP 8.1 or Later in PHP 8.0 with the Package Array Functions: PHP array functions that work in older versions

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-12-27 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 1 This week: 1All time: 11,525 This week: 35Up
Version License PHP version Categories
array-functions 1.0.0MIT/X Consortium ...8.0Language, PHP 8, Global
Description 

Author

This package provides PHP array functions that work in older versions.

It makes available several global functions built in PHP 8.1 and later that work in PHP 8.0.

Currently, it provides the following functions:

- array_find

- array_find_key

- array_any

- array_all

Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 30 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 11x

Documentation

Luminova Array Procedural Functions

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.

Install via Composer

Recommend installation method:

composer require luminovang/array-functions

Include File

You can also use the function in another projects.

include_once __DIR__ . '/vendor/luminovang/array-functions/src/ArrayFuncs.php;';

Importing the Functions

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;

Example Usage

Finding an Element in an Array

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.

Finding the Key of an Element in an Array

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'.

Checking If All Elements Meet a Condition

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.

Checking If Any Element Meets a Condition

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.


  Files folder image Files (5)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (5)  /  src  
File Role Description
  Accessible without login Plain text file ArrayFuncs.php Aux. Configuration script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:1
This week:1
All time:11,525
This week:35Up