note 92702 added to book.array

From: Date: Tue, 04 Aug 2009 17:45:52 +0000
Subject: note 92702 added to book.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-159103@lists.php.net to get a copy of this message
Made this function to delete elements in an array; <?php function array_del_elm($input_array, $del_indexes) { if (is_array($del_indexes)) { $indexes = $del_indexes; } elseif(is_string($del_indexes)) { $indexes = explode($del_indexes, " "); } elseif(is_numeric($del_indexes)) { $indexes[0] = (integer)$del_indexes; } else return; $del_indexes = null; $cur_index = 0; if (sort($indexes)) for($i=0; $i<count($input_array); $i++) { if ($i == $indexes[$cur_index]) { $cur_index++; if ($cur_index == count($indexes)) return $output_array; continue; } $output_array[] = $input_array[$i]; } return $output_array; } ?> but then i saw the methods of doing the same by Tyler Bannister & Paul, could see that theirs were faster, but had floors regarding deleting multiple elements thus support of several ways of giving parameters. I combined the two methods to this to this: <?php function array_del_elm($target_array, $del_indexes) { if (is_array($del_indexes)) { $indexes = $del_indexes; } elseif(is_string($del_indexes)) { $indexes = explode($del_indexes, " "); } elseif(is_numeric($del_indexes)) { $indexes[0] = (integer)$del_indexes; } else return; unset($del_indexes); for($i=0; $i<count($indexes); $i++) { unset($target_array[$indexes[$i]]); } return $target_array; } Fast, compliant and functional ;) ?> ---- Server IP: 195.184.96.15 Probable Submitter: 83.92.57.160 ---- Manual Page -- http://www.php.net/manual/en/book.array.php Edit -- https://master.php.net/note/edit/92702 Del: integrated -- https://master.php.net/note/delete/92702/integrated Del: useless -- https://master.php.net/note/delete/92702/useless Del: bad code -- https://master.php.net/note/delete/92702/bad+code Del: spam -- https://master.php.net/note/delete/92702/spam Del: non-english -- https://master.php.net/note/delete/92702/non-english Del: in docs -- https://master.php.net/note/delete/92702/in+docs Del: other reasons-- https://master.php.net/note/delete/92702 Reject -- https://master.php.net/note/reject/92702 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159103) next »