在Array中搜索元素

时间:2014-01-13 14:27:42

标签: php arrays

我有一个里面有数组的数组。我需要找到一个带有f.x的元素。 [' ID' => 9]然后从中获取国家/货币。我怎么能找到这样的元素?到目前为止,我循环并检查每个数组是否有我的ID,我相信它可以更轻松地完成。谢谢!

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

UPDATE 我希望能够通过任何键搜索:ID,国家,货币

5 个答案:

答案 0 :(得分:2)

最简单的方法是修复它,以便在构建数组时,将ID作为数组的键。所以它看起来像

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

然后你可以简单地做$countries[8]等等。

答案 1 :(得分:2)

如果您的数组的ID是唯一的,那么您可以使用array_filter(),例如:

$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'];

- 但如果id不是唯一的,则上面的代码将返回第一个找到的id的国家/地区。另请注意,仅在PHP> = 5.4中允许这种取消引用的方式(在早期版本中,您必须先找到数组元素,然后访问它的Country键)。

要按任意键搜索,您可以使用:

$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?

答案 2 :(得分:0)

类似于PHP multidimensional array search by value

[OR]

您可以将ID作为父数组的索引,并使用ID直接访问其值。 例如:

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

答案 3 :(得分:0)

现场演示:https://eval.in/88626
试试这个:

创建一个id为key的新数组,如下所示:

 $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;
}

按照这个

的ID访问你的数组
print_r($outArray[8]['Country']);

输出:

 Finland

搜索更新:
现场演示: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;
}

输出:

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
        )

)

答案 4 :(得分:0)

您可以创建一个应该为您完成工作的功能,并且它不会获得重复的ID。

工作示例: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);

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

输出

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

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