note 56441 deleted from ref.array by aidan

From: Date: Sun, 09 Oct 2005 09:12:28 +0000
Subject: note 56441 deleted from ref.array by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96726@lists.php.net to get a copy of this message
Note Submitter: paeppi ---- I slightly changed to function posted by "spam at afoyi dot com" to be able to delete elements from the middle of an array by value. I kept the original function also in here, but renamed it to my_array_delete_by_key(). <?php function my_array_delete_by_key($array, $item) { if (isset($array[$item])) unset($array[$item]); return array_merge($array); } function my_array_delete_by_value($array, $value) { $array_flip = array_flip($array); if (isset($array[$array_flip[$value]])) unset($array[$array_flip[$value]]); return array_merge($array); } ?> Here two examples how to use those functions: <?php $fruits = array( 'fruit1' => 'apple', 'fruit2' => 'orange', 'fruit3' => 'grape'); $fruits = my_array_delete_by_key($fruits, 'fruit2'); print_r($fruits); ?> This will display: Array ( [fruit1] => apple [fruit3] => grape ) <?php $fruits = array( 'fruit1' => 'apple', 'fruit2' => 'orange', 'fruit3' => 'grape'); $fruits = my_array_delete_by_value($fruits, 'orange'); print_r($fruits); ?> This will also display: Array ( [fruit1] => apple [fruit3] => grape )

« previous php.notes (#96726) next »