Oihana PHP

partition.php

Table of Contents

Functions

partition()  : array{0: array, 1: array}
Splits an array into two groups according to a predicate.

Functions

partition()

Splits an array into two groups according to a predicate.

partition(array<int|string, mixed> $items, callable $predicate) : array{0: array, 1: array}

The $predicate callback is invoked as fn( $value , $key ) for every entry. Entries for which it returns a truthy value go into the first group, the others into the second. Original keys are preserved in both groups.

Parameters
$items : array<int|string, mixed>

The array to split.

$predicate : callable

The test callback: fn( $value , $key ): bool.

Tags
example
use function oihana\core\arrays\partition;

[ $even , $odd ] = partition( [ 1 , 2 , 3 , 4 ] , fn( $n ) => $n % 2 === 0 ) ;
// $even === [ 1 => 2 , 3 => 4 ]
// $odd  === [ 0 => 1 , 2 => 3 ]
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
array{0: array, 1: array}

A pair [ 0 => $passed , 1 => $failed ].

On this page

Search results