用PHP搜索多维数组

用PHP搜索多维数组,php,arrays,search,multidimensional-array,Php,Arrays,Search,Multidimensional Array,如何在以下多维数组中搜索字符串“uk” array(43) { [0]=> array(1) { ["country"]=> string(9) "Australia" } [1]=> array(1) { ["country"]=> string(7) "Bahrain" } [2]=> array(1) { ["country"]=> string(7) "Belgium" } [3]=> array(1) { ["country"]

如何在以下多维数组中搜索字符串“uk”

array(43)
{
  [0]=> array(1) { ["country"]=> string(9) "Australia" }
  [1]=> array(1) { ["country"]=> string(7) "Bahrain" }
  [2]=> array(1) { ["country"]=> string(7) "Belgium" }
  [3]=> array(1) { ["country"]=> string(8) "Bulgaria" }
  [4]=> array(1) { ["country"]=> string(6) "Canada" }
  [5]=> array(1) { ["country"]=> string(7) "Croatia" }
  [6]=> array(1) { ["country"]=> string(6) "Cyprus" }
  [7]=> array(1) { ["country"]=> string(14) "Czech Republic" }
  [8]=> array(1) { ["country"]=> string(7) "Denmark" }
  [9]=> array(1) { ["country"]=> string(7) "Finland" }
  [10]=> array(1) { ["country"]=> string(7) "Germany" }
  [11]=> array(1) { ["country"]=> string(6) "Greece" }
  [12]=> array(1) { ["country"]=> string(9) "Hong Kong" }
  [13]=> array(1) { ["country"]=> string(7) "Hungary" }
  [14]=> array(1) { ["country"]=> string(7) "Iceland" }
  [15]=> array(1) { ["country"]=> string(5) "India" }
  [16]=> array(1) { ["country"]=> string(4) "Iran" }
  [17]=> array(1) { ["country"]=> string(6) "Israel" }
  [18]=> array(1) { ["country"]=> string(5) "Italy" }
  [19]=> array(1) { ["country"]=> string(5) "Japan" }
  [20]=> array(1) { ["country"]=> string(5) "Korea" }
  [21]=> array(1) { ["country"]=> string(6) "Kuwait" }
  [22]=> array(1) { ["country"]=> string(5) "Malta" }
  [23]=> array(1) { ["country"]=> string(8) "Mongolia" }
  [24]=> array(1) { ["country"]=> string(11) "Netherlands" }
  [25]=> array(1) { ["country"]=> string(6) "Norway" }
  [26]=> array(1) { ["country"]=> string(8) "Pakistan" }
  [27]=> array(1) { ["country"]=> string(6) "Poland" }
  [28]=> array(1) { ["country"]=> string(7) "Romania" }
  [29]=> array(1) { ["country"]=> string(6) "Russia" }
  [30]=> array(1) { ["country"]=> string(9) "Singapore" }
  [31]=> array(1) { ["country"]=> string(8) "Slovakia" }
  [32]=> array(1) { ["country"]=> string(12) "South Africa" }
  [33]=> array(1) { ["country"]=> string(13) "South America" }
  [34]=> array(1) { ["country"]=> string(5) "Spain" }
  [35]=> array(1) { ["country"]=> string(6) "Sweden" }
  [36]=> array(1) { ["country"]=> string(11) "Switzerland" }
  [37]=> array(1) { ["country"]=> string(6) "Taiwan" }
  [38]=> array(1) { ["country"]=> string(10) "Tajikistan" }
  [39]=> array(1) { ["country"]=> string(2) "UK" }
  [40]=> array(1) { ["country"]=> string(20) "United Arab Emirates" }
  [41]=> array(1) { ["country"]=> string(24) "United States of America" }
  [42]=> array(1) { ["country"]=> string(7) "Vietnam" }
}
1) 为什么要将所有简单字符串封装在一个新数组中

1a)如果确实有原因:

function my_search($needle, $haystack)
{
    foreach($haystack as $k => $v)
    {
        if ($v["country"] == $needle)
            return $k;
    }
    return FALSE;
}

$key = my_search("uk", $yourArray);
if($key !== FALSE)
{
    ...
} else {
    echo "Not Found.";
}
此函数可能返回布尔值FALSE,但也可能返回 计算结果为FALSE的非布尔值。请阅读关于 更多信息请参见布尔值。使用用于测试 此函数的返回值

1b)如果没有:

2) 使用简单字符串作为值,而不是字符串的数组(1),修复数组

3) 如果它是固定的,您可以简单地使用

编辑:更改参数以类似于数组。\u搜索更多。

1)为什么要将所有简单字符串封装在新数组中

1a)如果确实有原因:

function my_search($needle, $haystack)
{
    foreach($haystack as $k => $v)
    {
        if ($v["country"] == $needle)
            return $k;
    }
    return FALSE;
}

$key = my_search("uk", $yourArray);
if($key !== FALSE)
{
    ...
} else {
    echo "Not Found.";
}
此函数可能返回布尔值FALSE,但也可能返回 计算结果为FALSE的非布尔值。请阅读关于 更多信息请参见布尔值。使用用于测试 此函数的返回值

1b)如果没有:

2) 使用简单字符串作为值,而不是字符串的数组(1),修复数组

3) 如果它是固定的,您可以简单地使用

编辑:更改参数以更类似于数组搜索。

如果使用(PHP 5>=5.5.0),则有一种更好、更简单的方法:

if(数组搜索('UK',数组列($array,'country'))!==false){
回声“发现”;
}否则{
回声“未找到”;
}
如果您正在使用(PHP 5>=5.5.0),有一种更好、更简单的方法:

if(数组搜索('UK',数组列($array,'country'))!==false){
回声“发现”;
}否则{
回声“未找到”;
}

能否改进代码格式。能否改进代码格式。