note 103811 added to function.array-filter

From: Date: Wed, 04 May 2011 21:01:18 +0000
Subject: note 103811 added to function.array-filter
Groups: php.notes 
Request: Send a blank email to php-notes+get-179430@lists.php.net to get a copy of this message
Hi all, I made a simple reindex & filter function. reindex( array $input, [ $blacklist = array() ] ) This function is working only with array that has keys in INTEGER. <?php function reindex(array $source, $blacklist = array()) { $i = 0; foreach ($source as $key => $val) { if ($key != $i) { unset($source[$key]); $source[$i] = $val; } $i++; } foreach ($source as $key => $val) { foreach ($blacklist as $var) { if ($val === $var) { unset($source[$key]); $source = reindex($source, $blacklist); } } } return $source; } // Examples: // Create a simple array $input = array(1 => 'red', 3 => 'green', 5 => 'blue', 7 => TRUE, 9 => FALSE, 11 => NULL); // NOTE: If you have an array (look above) and your blacklist will look like this: array(TRUE, FALSE, NULL) // Filter will delete array keys with value TRUE, FALSE or NULL $blacklist = array(TRUE, FALSE, NULL); // Output is: Array ( [0] => red [1] => green [2] => blue ) print_r( reindex($input, $blacklist) ); ?> If you don't want to filter array by keys in blacklist, just use this prototype: reindex( array $input ), it should be fine. :) If you guys have question, feel free to contact me. Have a nice day. ---- Server IP: 82.119.225.41 Probable Submitter: 212.26.185.229 ---- Manual Page -- http://www.php.net/manual/en/function.array-filter.php Edit -- https://master.php.net/note/edit/103811 Del: integrated -- https://master.php.net/note/delete/103811/integrated Del: useless -- https://master.php.net/note/delete/103811/useless Del: bad code -- https://master.php.net/note/delete/103811/bad+code Del: spam -- https://master.php.net/note/delete/103811/spam Del: non-english -- https://master.php.net/note/delete/103811/non-english Del: in docs -- https://master.php.net/note/delete/103811/in+docs Del: other reasons-- https://master.php.net/note/delete/103811 Reject -- https://master.php.net/note/reject/103811 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179430) next »