note 81973 added to function.array-slice

From: Date: Fri, 21 Mar 2008 20:51:06 +0000
Subject: note 81973 added to function.array-slice
Groups: php.notes 
Request: Send a blank email to php-notes+get-137310@lists.php.net to get a copy of this message
array_slice can be used to remove elements from an array but it's pretty simple to use a custom function. One day array_remove() might become part of PHP and will likely be a reserved function name, hence the unobvious choice for this function's names. <? function arem($array,$value){ $holding=array(); foreach($array as $k => $v){ if($value!=$v){ $holding[$k]=$v; } } return $holding; } function akrem($array,$key){ $holding=array(); foreach($array as $k => $v){ if($key!=$k){ $holding[$k]=$v; } } return $holding; } $lunch = array('sandwich' => 'cheese', 'cookie'=>'oatmeal','drink' => 'tea','fruit' => 'apple'); echo '<pre>'; print_r($lunch); $lunch=arem($lunch,'apple'); print_r($lunch); $lunch=akrem($lunch,'sandwich'); print_r($lunch); echo '</pre>'; ?> (remove 9's in email) ---- Server IP: 208.69.120.35 Probable Submitter: 209.194.189.187 ---- Manual Page -- http://www.php.net/manual/en/function.array-slice.php Edit -- https://master.php.net/note/edit/81973 Del: integrated -- https://master.php.net/note/delete/81973/integrated Del: useless -- https://master.php.net/note/delete/81973/useless Del: bad code -- https://master.php.net/note/delete/81973/bad+code Del: spam -- https://master.php.net/note/delete/81973/spam Del: non-english -- https://master.php.net/note/delete/81973/non-english Del: in docs -- https://master.php.net/note/delete/81973/in+docs Del: other reasons-- https://master.php.net/note/delete/81973 Reject -- https://master.php.net/note/reject/81973 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137310) next »