Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_filter with assoc array?

I am using array_filter to do something like this:

function endswithy($value) {
    return (substr($value, -1) == 'y');
}

$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
$withy = array_filter($people, "endswithy");
var_dump($withy);

BUT with the more option in filter for example

$people = array(
             "Johnny"=>array("year"=>1989, "job"=>"prof"),
             "Timmy"=>array("year"=>1989,  "job"=>"std"),
             "Bobby"=>array("year"=>1988),
             "Sam"=>array("year"=>1983),
             "Tammy"=>array("year"=>1985),
             "Danny"=>array("year"=>1983),
             "Joe"=>array("year"=>1989,"job"=>"prof"));

OR

$people = array(
             array("name"=>"Johnny","year"=>1989, "job"=>"prof"),
             array("name"=>"Timmy","year"=>1989,  "job"=>"std"),
             array("name"=>"Bobby""year"=>1988),
             array("name"=>"Sam","year"=>1983),
             array("name"=>"Tammy","year"=>1985),
             array("name"="Danny","year"=>1983),
             array("name"="Joe","year"=>1989,"job"=>"prof"));

How Can I got the only this people (endwith y and year=1989 and job=prof) ,Can I use array_filter? or any build-in function to do this?

$people = array(
                 "Johnny"=>array("year"=>1989, "job"=>"prof")
  );

OR

$people = array(
                 array("name="Johnny","year"=>1989, "job"=>"prof")
  );
like image 598
newbie Avatar asked Jun 01 '11 10:06

newbie


People also ask

Does In_array work for associative array?

in_array() function is utilized to determine if specific value exists in an array. It works fine for one dimensional numeric and associative arrays.

What is array associative array with syntax with example?

Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order. Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

How do you filter an array from all elements of another array in PHP?

The array_filter() function filters the values of an array using a callback function. This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

What is multidimensional associative array?

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.


1 Answers

PHP 5.6 introduces the optional flag ARRAY_FILTER_USE_KEY that will allow this:

function endswithy($name) {
    return (substr($name, -1) == 'y');
}

$people = array(
     "Johnny"=>array("year"=>1989, "job"=>"prof"),
     "Timmy"=>array("year"=>1989,  "job"=>"std"),
     "Bobby"=>array("year"=>1988),
     "Sam"=>array("year"=>1983),
     "Tammy"=>array("year"=>1985),
     "Danny"=>array("year"=>1983),
     "Joe"=>array("year"=>1989,"job"=>"prof")
);

$peopleEndingInY = array_filter($people, 'endswithy', ARRAY_FILTER_USE_KEY);

// Outputs: 5
var_dump(count($peopleEndingInY));

If you need to maintain and key and the value, another flag ARRAY_FILTER_USE_BOTH will do that as seen in this example:

$ar = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

//Note that this doens't actually filter anything since it doesn't return a bool.
$output = array_filter($ar, function($value, $key){
    echo sprintf("%s => %s\n", $key, $value);
}, ARRAY_FILTER_USE_BOTH);
like image 85
HPierce Avatar answered Oct 06 '22 15:10

HPierce