Well, now we're getting to be complex again.
But for the sake of exercise...
PHP Code:
/**
* Filters a multi-dimentional array based on matching (or not)
* a key/value pair of the items
*
* @param array $data
* @param string $k
* @param string $v
* @param boolean $match
*
* @return array
*/
function special_array_filter( $data, $k, $v, $match = true ) {
$filter = function ( $item ) use ( $k, $v, $match ) {
return ( isset( $item[ $k ] ) && $item[ $k ] == $v ) ? $match : !$match;
};
return array_filter( $data, $filter );
}
// usage:
$orig_array = array(
array (
"id" => 11,
"rule"=> "rule 1"
),
array (
"id" => 40,
"rule"=> "rule 2"
),
array (
"id" => 55,
"rule"=> "rule 3"
),
array (
"id" => 40,
"rule"=> "rule 4"
) );
$new_array = special_array_filter( $orig_array, 'id', 40, false );
Don't try this at home - unless you need to do something like that in more than one place. But really, just go with the foreach thing, though I do like my first array_filter solution myself.
Bookmarks