Remove a specific item from an array in PHP

Posted on Oct 01, 2020
function my_remove_array_item( $array, $item ) {
	$index = array_search($item, $array);
	if ( $index !== false ) {
		unset( $array[$index] );
	}

	return $array;
}

Usage

$items = array( 'first', 'second', 'third');
$items = my_remove_array_item( $items, 'second' ); // remove item called 'second'
guest
0 Comments
Inline Feedbacks
View all comments