Skip to content

array_pull

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

The array_pull() function walks through the given array. When it finds the requested element (via the key(s)), it removes it. As the array is being worked on by reference, it changes the given array. Then it returns the removed element's value.

Pay attention as this function does multiple tasks:

  1. Permanently deletes the element out of the given array. The array you pass in will be changed.
  2. That element is returned to you in case you want to use it for something.

Syntax

mixed array_pull( 
     array &$subjectArray,
     string $key,
     [ mixed $default ]
);

Examples:

$user = array(
	'user_id'   => 504,
	'name'      => 'Bob Jones',
	'social'    => array(
		'twitter' => '@bobjones',
   		'website' => 'https://bobjones.com',
	),
);

$twitter_handle = array_pull( $user, 'social.twitter' );
// Returns: `@bobjones`

For the above example, the array $user now holds the following values:

/**
  array(
   	'user_id'   => 504,
   	'name'      => 'Bob Jones',
   	'social'    => array(
   		'website' => 'https://bobjones.com',
   	),
   )
*/   

Notice that social.twitter is gone. It's been deleted.


« Back to Array API

Clone this wiki locally