Oihana PHP

filter.php

Table of Contents

Functions

filter()  : object
Keeps the public properties of an object that satisfy a predicate.

Functions

filter()

Keeps the public properties of an object that satisfy a predicate.

filter(object $object, callable $fn) : object

Returns a new stdClass containing only the properties for which fn( $value , $key ) returns a truthy value. The source object is never modified.

Parameters
$object : object

The source object.

$fn : callable

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

Tags
example
use function oihana\core\objects\filter;

$values = (object) [ 'a' => 1 , 'b' => 2 , 'c' => 3 ] ;

$result = filter( $values , fn( $v ) => $v % 2 === 1 ) ;
// (object) [ 'a' => 1 , 'c' => 3 ]
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
object

A new stdClass with only the kept properties.

On this page

Search results