Oihana PHP

pick.php

Table of Contents

Functions

pick()  : object
Keeps only the specified public properties of an object.

Functions

pick()

Keeps only the specified public properties of an object.

pick(object $object, array<int, string> $keys) : object

Returns a new stdClass containing only the listed properties that actually exist on the source object. The source object is never modified. Keys that are absent are silently ignored.

Parameters
$object : object

The source object.

$keys : array<int, string>

The list of property names to keep.

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

$user = (object) [ 'id' => 42 , 'name' => 'Alice' , 'email' => 'alice@example.com' ] ;

$result = pick( $user , [ 'id' , 'name' ] ) ;
// (object) [ 'id' => 42 , 'name' => 'Alice' ]
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
object

A new stdClass with only the picked properties.

On this page

Search results