Skip to content

array_get

Rose Cox edited this page Mar 9, 2017 · 2 revisions

The array_get() function walks through the array to fetch the value of the element you specify via the key. The key can be in dot notation, a single key, or an array of keys.

Syntax:

mixed array_get( 
     array $subject_array, 
     string|array $key,
     [ mixed $default_value ] 
);

Examples:

$user = array(
	'user_id'   => 504,
	'name'      => 'Bob Jones',
	'social'    => array(
		'twitter' => '@bobjones',
	),
);

$twitter_handle = array_get( $user, 'social.twitter' );
$twitter_handle = array_get( $user, array( 'social', 'twitter' ) );
// '@bobjones'

$user_id = array_get( $user, 'user_id' );
// 504

You can also specify a default value to be returned in the event the array does not have the specified element.

$value = array_get( $user, 'email', false );
// false

$value = array_get( $user, 'email' );
// null

« Back to Array API

Clone this wiki locally