Created
November 9, 2020 11:20
-
-
Save alpenzoo/40c28a9dcfdc4a1f2d68b41ce670f9ad to your computer and use it in GitHub Desktop.
php function returns array filtered on multiple attributes values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$arr= array( | |
array("an"=>2, 'fac'=>5000, 'sr'=>45), | |
array("an"=>4, 'fac'=>5001, 'sr'=>40), | |
array("an"=>2, 'fac'=>5000, 'sr'=>40), | |
array("an"=>2, 'fac'=>5001, 'sr'=>45) | |
); | |
$cond=array("an"=>2, 'fac'=>5001); | |
function ucv_arrayFind($arr, $cond){ | |
$b = array(); | |
array_walk($arr, function($a) use(&$b,$cond) { | |
//echo '<pre>'; print_r($a);echo '</pre>'; | |
//print_r($cond); | |
foreach ($cond as $k => $v) { | |
if($a[$k]!==$v){ | |
return; | |
} | |
} | |
$b[] = $a; | |
}); | |
return $b; | |
} | |
echo '<pre>'; print_r(ucv_arrayFind($arr, $cond));echo '</pre>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment