Skip to content

array_remove

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

The array_remove() function removes one or more elements from the given array. You use this function when you want to remove or delete specific elements from an array.

For deeply nested arrays, use dot notation. When you want more than one element removed, put their keys into an array as the second argument.

Syntax

void array_remove( 
     array &$subjectArray
     string|array $keys
);

Examples

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

array_remove( $user, 'social.website' );

The array $user is changed to:

/**
  array(
   	'user_id'   => 504,
   	'name'      => 'Bob Jones',
   	'social'    => array(
   		'twitter' => '@bobjones',
   	),
   )
*/   

Notice, that the website element within the social level is gone. It's been deleted.

Another Example

In this example, we are removing developer3 from both lists, i.e. from names and emails.

$developers  = array(
	'names'  => array(
		'developer1' => 'Tonya',
		'developer2' => 'Rose',
		'developer3' => 'Nate',
	),
	'emails' => array(
		'developer1' => 'foo',
		'developer2' => 'bar',
		'developer3' => 'baz',
	),
);

array_remove( $developers, array( 'names.developer3', 'emails.developer3' ) );

The $developers array is then changed to:

$developers  = array(
	'names'  => array(
		'developer1' => 'Tonya',
		'developer2' => 'Rose',
	),
	'emails' => array(
		'developer1' => 'foo',
		'developer2' => 'bar',
	),
);

« Back to Array API

Clone this wiki locally