Skip to content

array_get_only

Tonya Mork edited this page Mar 12, 2017 · 1 revision

The array_get_only() function gets a subset of the elements from the given array. You can pass a single key or an array of keys to specify the subset to fetch.

Note: This function does not use dot notation.

Syntax

array array_get_only( 
     array $subjectArray, 
     string|array $keys
);

Where:

$subjectArray is the array to work on and contains the subset you want

$keys is a single key or an array of keys for the subset you want

Example

$user = array(
	'user_id'   => 504,
	'name'      => 'Bob Jones',
	'social'    => array(
		'twitter' => '@bobjones',
		'website' => 'https://bobjones.com',
	),
	'languages' => array(
		'php'        => array(
			'procedural' => true,
			'oop'        => false,
		),
		'javascript' => true,
		'ruby'       => false,
	),
);

$user = array_get_only( $user, array( 'user_id', 'name' ) );
// Returns: Bob Jones

$user = array_get_only( $user, 'name' );
/**
   Returns:

$user = array(
	'user_id'   => 504,
	'name'      => 'Bob Jones',
);*/

« Back to Array API

Clone this wiki locally