note 56441 added to ref.array

From: Date: Fri, 02 Sep 2005 14:59:15 +0000
Subject: note 56441 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-94664@lists.php.net to get a copy of this message
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 ) ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56441 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56441&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56441&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56441&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56441&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56441&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56441&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56441&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94664) next »