Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Search an Array for multiple key / value pairs

Tags:

I have a array list (for this example I'm using cell phones). I'm wanting to be able to search for multiple key/value pairs and return it's parent array index.

For example, here is my array:

// $list_of_phones (array)
Array
(
    [0] => Array
        (
            [Manufacturer] => Apple
            [Model] => iPhone 3G 8GB
            [Carrier] => AT&T
        )

    [1] => Array
        (
            [Manufacturer] => Motorola
            [Model] => Droid X2
            [Carrier] => Verizon
        )
)

I'm wanting to be able to do something like the following:

// This is not a real function, just used for example purposes
$phone_id = multi_array_search( array('Manufacturer' => 'Motorola', 'Model' => 'Droid X2'), $list_of_phones );

// $phone_id should return '1', as this is the index of the result.

Any ideas or suggestions on how I can or should do this?

like image 580
Wes Foster Avatar asked Dec 17 '12 22:12

Wes Foster


People also ask

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

How do you check if a key exists in an array PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

What is use of In_array () function in PHP?

PHP in_array() Function The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.


2 Answers

Perhaps this will be useful:

  /**
   * Multi-array search
   *
   * @param array $array
   * @param array $search
   * @return array
   */
  function multi_array_search($array, $search)
  {

    // Create the result array
    $result = array();

    // Iterate over each array element
    foreach ($array as $key => $value)
    {

      // Iterate over each search condition
      foreach ($search as $k => $v)
      {

        // If the array element does not meet the search condition then continue to the next element
        if (!isset($value[$k]) || $value[$k] != $v)
        {
          continue 2;
        }

      }

      // Add the array element's key to the result array
      $result[] = $key;

    }

    // Return the result array
    return $result;

  }

  // Output the result
  print_r(multi_array_search($list_of_phones, array()));

  // Array ( [0] => 0 [1] => 1 )

  // Output the result
  print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple')));

  // Array ( [0] => 0 )

  // Output the result
  print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple', 'Model' => 'iPhone 6')));

  // Array ( )

As the output shows, this function will return an array of all keys with elements which meet all the search criteria.

like image 166
MichaelRushton Avatar answered Sep 19 '22 21:09

MichaelRushton


you may use array_intersect_key and array_intersect and array_search

check array_intersect_key php manual to get array of items with matching keys

and array_intesect php manual to get array if items with matching values

u can get value of key in array using $array[key]

and get key of value in array using array_search $key = array_search('green', $array);

php.net/manual/en/function.array-search.php

like image 45
Khalil Awada Avatar answered Sep 21 '22 21:09

Khalil Awada