note 71426 added to ref.array

From: Date: Tue, 28 Nov 2006 15:09:22 +0000
Subject: note 71426 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-120127@lists.php.net to get a copy of this message
Ever wanted to obtain a subset of an associative array from a list of parameters (stored into a non-associative array)? Give this a try: <?php $params = array("name" => "Simone", "gender" => "M", "city" => "Macerata", "phone" => 123); $fields = array("name", "city"); echo "<pre>"; print_r($params); print_r($fields); print_r(subArr($params, $fields)); function subArr($assocArrHaystack, $arrFields) { $arrSub = array(); foreach ($assocArrHaystack as $key => $value) { if (in_array($key, $arrFields)) { $arrSub[$key] = $value; } } return $arrSub; } ?> And the output is: Array ( [name] => Simone [gender] => M [city] => Macerata [phone] => 123 ) Array ( [0] => name [1] => city ) Array ( [name] => Simone [city] => Macerata ) Enjoy! Simone ---- Server IP: 80.241.173.254 Probable Submitter: 193.205.125.10 (proxied: 193.205.113.27) ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/71426 Del: integrated -- https://master.php.net/note/delete/71426/integrated Del: useless -- https://master.php.net/note/delete/71426/useless Del: bad code -- https://master.php.net/note/delete/71426/bad+code Del: spam -- https://master.php.net/note/delete/71426/spam Del: non-english -- https://master.php.net/note/delete/71426/non-english Del: in docs -- https://master.php.net/note/delete/71426/in+docs Del: other reasons-- https://master.php.net/note/delete/71426 Reject -- https://master.php.net/note/reject/71426 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120127) next »