Dmytro Pastovenskyi
Dmytro Pastovenskyi

Reputation: 5419

Search for an element in Array

I've an array with arrays inside. I need to find an element with f.x. ['ID' => 9] and then get Country/Currency from it. How could I find such element? So far I do cycle and check every array for my ID, I believe it is possible to do it more easy. Thanks!

$countries = array(
    array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
    array('ID' => '9','Country' => 'France','Currency' => 'EUR'),
    ...
);

UPDATE I want to be able search by any key: ID, Country, Currency

Upvotes: 1

Views: 183

Answers (5)

Awlad Liton
Awlad Liton

Reputation: 9351

Live demo: https://eval.in/88626
Try this:

create a new array with id as key like this:

 $countries = array(
    array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
    array('ID' => '9','Country' => 'France','Currency' => 'EUR')
);
$outArray = array();
foreach($countries as $country){
 $outArray[$country['ID']] = $country;
}

access your array by its id like this

print_r($outArray[8]['Country']);

Output:

 Finland

Update for search:
Live demo : https://eval.in/88651

$countries = array(
        array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
        array('ID' => '9','Country' => 'France','Currency' => 'EUR')
    );
$r1 = getArrayById($countries,8);
$r2 = getArrayById($countries,'Finland');
$r3 = getArrayById($countries,'EUR');

  print_r($r1);
  print_r($r2);
  print_r($r3);

function  getArrayById($countries,$val){
 $outArray = array();
  foreach($countries as $country){
     if(in_array($val,$country)){
       $outArray[$country['ID']] = $country;
      }
    }
    return $outArray;
}

Output:

Array
(
    [8] => Array
        (
            [ID] => 8
            [Country] => Finland
            [Currency] => EUR
        )

)


Array
(
    [8] => Array
        (
            [ID] => 8
            [Country] => Finland
            [Currency] => EUR
        )

)


Array
(
    [8] => Array
        (
            [ID] => 8
            [Country] => Finland
            [Currency] => EUR
        )

    [9] => Array
        (
            [ID] => 9
            [Country] => France
            [Currency] => EUR
        )

)

Upvotes: 0

Jorge Faianca
Jorge Faianca

Reputation: 791

You can create a function that should do the work for you, And it does not get duplicated ids.

Working example: example

$countries = array(
    array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
    array('ID' => '9','Country' => 'France','Currency' => 'EUR')
);

echo '<pre>';

function find($word, $countries, $result = null){

    foreach($countries as $k => $v){

        if(in_array($word, $v)){
            $result[$v['ID']] = $v; // Do not get duplicated id's
        }
    }
    return $result;
};

$result = array();

$result = find('Finland', $countries); // Get's ID 8
$result = find('EUR', $countries, $result); //  Get's id 8 and 9, don't duplicate
$result = find('9', $countries, $result); // Get ids 9 but don't duplicate

print_r($result);

OUTPUT

Array
(
    [8] => Array
        (
            [ID] => 8
            [Country] => Finland
            [Currency] => EUR
        )

    [9] => Array
        (
            [ID] => 9
            [Country] => France
            [Currency] => EUR
        )
)

Upvotes: 0

codepiper
codepiper

Reputation: 109

similar to PHP multidimensional array search by value

[OR]

You can make ID as the index of parent array and access its values directly using ID. example:

$countries = array(
8 => array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
9 => array('ID' => '9','Country' => 'France','Currency' => 'EUR'),
...
);

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

If your array's id are unique, then you can use array_filter(), like:

$countries = array(
    array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
    array('ID' => '9','Country' => 'France','Currency' => 'EUR')
);

$id = 9;
$result = array_shift(array_filter($countries, function($item) use ($id)
{
   return $item['ID']==$id;
}))['Country'];

-but if id's are non-unique, code above will return country for first found id. Also note that such way of de-reference is allowed in PHP>=5.4 only (in earlier versions you'll have to find array element first, then access it's Country key).

To search by any key you can use:

$key   = 'ID';
$value = 9;
$result = array_shift(array_filter($countries, function($item) use ($key, $value)
{
   //change == to === for strict comparison:
   return array_key_exists($key, $item) && $item[$key]==$value;
})); //whole element, not only 'Country' key what cause if search will be by Country?

Upvotes: 3

Jessica
Jessica

Reputation: 7005

The easiest way would be to fix it so when you build the array, you put the ID as the array's key. So it would look like

$countries = array(
    8=>array('ID' => '8','Country' => 'Finland','Currency' => 'EUR'),
    9=>array('ID' => '9','Country' => 'France','Currency' => 'EUR'),
    ...
);

Then you can simple do $countries[8], etc.

Upvotes: 2

Related Questions