note 93965 added to function.array-filter
| From: | webdesign at blackbyrd dot biz | Date: | Thu, 08 Oct 2009 20:00:24 +0000 |
| Subject: | note 93965 added to function.array-filter | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161569@lists.php.net to get a copy of this message | ||
Here's a function that will filter a multi-demensional array. This filter will return only
those items that match the $value given
/*
* filtering an array
*/
function filter_by_value ($array, $index, $value){
if(is_array($array) && count($array)>0)
{
foreach(array_keys($array) as $key){
$temp[$key] = $array[$key][$index];
if ($temp[$key] == $value){
$newarray[$key] = $array[$key];
}
}
}
return $newarray;
}
Example:
$results = array(
0 => array('key1' => '1', 'key2' => 2, 'key3'
=> 3),
1 => array('key1' => '12', 'key2' => 22, 'key3'
=> 32)
);
$nResults = filter_by_value($results, 'key2', '2');
Output :
array(
0 => array('key1' => '1', 'key2' => 2, 'key3'
=> 3)
);
----
Server IP: 69.147.83.197
Probable Submitter: 98.247.229.102
----
Manual Page -- http://www.php.net/manual/en/function.array-filter.php
Edit -- https://master.php.net/note/edit/93965
Del: integrated -- https://master.php.net/note/delete/93965/integrated
Del: useless -- https://master.php.net/note/delete/93965/useless
Del: bad code -- https://master.php.net/note/delete/93965/bad+code
Del: spam -- https://master.php.net/note/delete/93965/spam
Del: non-english -- https://master.php.net/note/delete/93965/non-english
Del: in docs -- https://master.php.net/note/delete/93965/in+docs
Del: other reasons-- https://master.php.net/note/delete/93965
Reject -- https://master.php.net/note/reject/93965
Search -- https://master.php.net/manage/user-notes.php