如果匹配则从数组中获取值

时间:2013-09-19 16:09:55

标签: php arrays php-5.3

我有时会将阵列作为一个位置或多个位置返回 我想用变量$ location = egypt检查数组; 如果找到此值,则从所有这些位置数组中重新调整此数组

stdClass Object
(
    [count] => 2
    [query] => Cairo
    [locations] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1190
                    [name] => Cairo, Egypt - 341 hotels
                    [country_name] => Egypt
                    [country_code] => EG
                    [state_name] => 
                    [state_code] => 
                )

            [1] => stdClass Object
                (
                    [id] => 11016
                    [name] => Cairo, GA, United States - 4 hotels
                    [country_name] => United States
                    [country_code] => US
                    [state_name] => {"en":"Georgia"}
                    [state_code] => GA
                )

        )

)

2 个答案:

答案 0 :(得分:0)

function locationReturn($locationArray){
  $result = array();
  foreach($locationArray as $value){
    if($value['country_name'] == 'Egypt')
     return $value;
  }
  return $result; // return empty array if not getting
}

答案 1 :(得分:0)

这是一个经常出现的问题。例如,您可以找到答案here