Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7 - How to search for multiple specific members of an array

I have a PHP array that looks like this:

array (size=3)
  'CA,Santa Barbara' => 
    array (size=2)
      'state' => string 'CA' (length=2)
      'city' => string 'Santa Barbara' (length=13)
  'KS,Andover' => 
    array (size=2)
      'state' => string 'KS' (length=2)
      'city' => string 'Andover' (length=7)
  'KS,Wichita' => 
    array (size=2)
      'state' => string 'KS' (length=2)
      'city' => string 'Wichita' (length=7)

I need to return all the array members who have the values:

state => 'KS'

I am needing to get an array back that looks like this:

array (size=2)
  'KS,Andover' => 
    array (size=2)
      'state' => string 'KS' (length=2)
      'city' => string 'Andover' (length=7)
  'KS,Wichita' => 
    array (size=2)
      'state' => string 'KS' (length=2)
      'city' => string 'Wichita' (length=7)

Is there a PHP array function that does this? I have found array_search() but that only returns the first match. I need ALL matches.

like image 877
Eric Snyder Avatar asked Mar 25 '18 19:03

Eric Snyder


People also ask

How do you check for multiple values in an array?

To check if multiple values exist in an array:Use the every() method to iterate over the array of values. On each iteration, use the indexOf method to check if the value is contained in the other array. If all values exist in the array, the every method will return true .

How do you check if multiple values are in an array PHP?

To see if an element exists in the array, use the array_key_exists( ) function: if (array_key_exists(key, array)) { ... } The function returns a Boolean value that indicates whether the second argument is a valid key in the array given as the first argument.

How to access PHP array elements?

Accessing Elements in a PHP Array The elements in a PHP numerical key type array are accessed by referencing the variable containing the array, followed by the index into array of the required element enclosed in square brackets ([]).

How to find common elements in two arrays in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.


1 Answers

You could use array_filter() to remove all unneeded values:

$array = [
    'CA,Santa Barbara' => ['state' => 'CA', 'city' => 'Santa Barbara'],
    'KS,Andover'       => ['state' => 'KS', 'city' => 'Andover'],
    'KS,Wichita'       => ['state' => 'KS', 'city' => 'Wichita'],
];

$state = 'KS';
$out   = array_filter($array, fn($v) => $v['state'] == $state);
print_r($out);

Outputs:

Array(
    [KS,Andover] => Array(
            [state] => KS
            [city] => Andover
        )
    [KS,Wichita] => Array(
            [state] => KS
            [city] => Wichita
        )
)

You can also use type hinting to get a better data flow control:

$out = array_filter($array, fn(array $v): bool => $v['state'] == $state);
#                              ^          ^
#                              |          |
#                              |          array_filter's callback returns
#                              |          a boolean (true to keep,
#                              |          false to remove).
#                              |
#                              each elements of the $array
#                              are arrays.

Before PHP 7.4, (without arrow functions), you have to use the keyword use to pass the $state variable to the anonymous function:

$state = 'KS';
$out   = array_filter($array, function($v) use ($state) {
    return $v['state'] == $state;
});
print_r($out);
like image 158
Syscall Avatar answered Nov 04 '22 11:11

Syscall