1

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

Dmytro Pastovenskyi
  • 5,240
  • 5
  • 37
  • 56

5 Answers5

3

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?
Alma Do
  • 37,009
  • 9
  • 76
  • 105
2

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.

Jessica
  • 7,075
  • 28
  • 39
  • Nah, would be `$countries[8]['id']` or `$countries[8]['Country']`, it's multi dimensional :) – Mr. Alien Jan 13 '14 at 14:30
  • 1
    @Mr.Alien That would be if you wanted a specific key in that array. You could get the entire array by doing `$countries[8]`. See how you did that as the first part of each of your examples? *smh* – Jessica Jan 13 '14 at 14:31
  • Yes obviously, but the next question would be, now am trying to `echo` but it doesn't work cuz he wrote *and then get Country/Currency from it.* – Mr. Alien Jan 13 '14 at 14:36
  • Since the OP already has working code that does this, I'm going to assume he knows how to access values in a multi-dimensional array. I didn't say to echo $countries[8], I said "do $countries[8], etc" which is just a generic statement about accessing the array. Adding the comment that to echo it you would need another key is fine, but you posted it as if my statement that you can access the array via $counties[8] is wrong, which it is not. – Jessica Jan 13 '14 at 14:47
0

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'),
...
);
Community
  • 1
  • 1
codepiper
  • 129
  • 10
0

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
        )

)
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0

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
        )
)

Jorge Faianca
  • 791
  • 5
  • 11