note 101422 added to function.array-filter

From: Date: Wed, 15 Dec 2010 19:19:05 +0000
Subject: note 101422 added to function.array-filter
Groups: php.notes 
Request: Send a blank email to php-notes+get-174963@lists.php.net to get a copy of this message
I built the following array_remove_keys() function to remove one or more keys from an array. You can also find it on my blog at: http://www.reich-consulting.net/?p=429 <?php function array_remove_keys($array, $keys = array()) { // If array is empty or not an array at all, don't bother // doing anything else. if(empty($array) || (! is_array($array))) { return $array; } // If $keys is a comma-separated list, convert to an array. if(is_string($keys)) { $keys = explode(',', $keys); } // At this point if $keys is not an array, we can't do anything with it. if(! is_array($keys)) { return $array; } // array_diff_key() expected an associative array. $assocKeys = array(); foreach($keys as $key) { $assocKeys[$key] = true; } return array_diff_key($array, $assocKeys); } // Example: $data = array( 'name' => 'Brian', 'address1' => '98 Market St.', 'address2' => 'N/A' ); // Output before array_remove_keys() var_dump($data); // Remove address2 key. $data = array_remove_keys($data, 'address2'); // Output after array_remove_keys() var_dump($data); /* Output: array(3) { ["name"]=> string(5) "Brian" ["address1"]=> string(13) "98 Market St." ["address2"]=> string(3) "N/A" } array(2) { ["name"]=> string(5) "Brian" ["address1"]=> string(13) "98 Market St." } */ ---- Server IP: 69.147.83.197 Probable Submitter: 209.50.143.75 ---- Manual Page -- http://www.php.net/manual/en/function.array-filter.php Edit -- https://master.php.net/note/edit/101422 Del: integrated -- https://master.php.net/note/delete/101422/integrated Del: useless -- https://master.php.net/note/delete/101422/useless Del: bad code -- https://master.php.net/note/delete/101422/bad+code Del: spam -- https://master.php.net/note/delete/101422/spam Del: non-english -- https://master.php.net/note/delete/101422/non-english Del: in docs -- https://master.php.net/note/delete/101422/in+docs Del: other reasons-- https://master.php.net/note/delete/101422 Reject -- https://master.php.net/note/reject/101422 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174963) next »