获取多维数组的关键

时间:2013-12-30 18:10:25

标签: php arrays multidimensional-array

我认为这很容易,但我似乎无法做到这一点

以下是我的数组

Array ( [0] => Array ( [countryid] => 38 )
        [1] => Array ( [currency] => GBP [countryid] => 109 )
        [2] => Array ( [currency] => GBP [countryid] => 136 )
        [3] => Array ( [currency] => GBP [countryid] => 94 )
        [4] => Array ( [currency] => GBP [countryid] => )
        [5] => Array ( [currency] => GBP [countryid] => 131 )
        [6] => Array ( [currency] => USD [countryid] => 5 )
        [7] => Array ( [currency] => USD [countryid] => 22 )
        [8] => Array ( [currency] => USD [countryid] => 1 )
        [9] => Array ( [currency] => USD [countryid] => 2 )
        [10] => Array ( [currency] => USD [countryid] => 3 )
        [11] => Array ( [currency] => USD [countryid] => 4 )
        [12] => Array ( [currency] => USD [countryid] => 7 )
        [13] => Array ( [currency] => USD [countryid] => 8 )
        [14] => Array ( [currency] => USD [countryid] => 9 )
        [15] => Array ( [currency] => USD [countryid] => 10 )
        [16] => Array ( [currency] => USD [countryid] => 11 )
        [17] => Array ( [currency] => USD [countryid] => 12 )
        [18] => Array ( [currency] => USD [countryid] => 14 )
        [19] => Array ( [currency] => USD [countryid] => 15 )
        [20] => Array ( [currency] => USD [countryid] => 16 )
        [21] => Array ( [currency] => USD [countryid] => 21 )
        [22] => Array ( [currency] => USD [countryid] => 22 )
        [23] => Array ( [currency] => USD [countryid] => 23 )
        [24] => Array ( [currency] => USD [countryid] => 24 )
        [25] => Array ( [currency] => USD [countryid] => 25 )
        [26] => Array ( [currency] => USD [countryid] => 26 )
        [27] => Array ( [currency] => USD [countryid] => 27 )
        [28] => Array ( [currency] => USD [countryid] => 28 )
        [29] => Array ( [currency] => USD [countryid] => 29 )
        [30] => Array ( [currency] => USD [countryid] => 30 )
        [31] => Array ( [currency] => USD [countryid] => 31 )
        [32] => Array ( [currency] => USD [countryid] => 34 )
        [33] => Array ( [currency] => USD [countryid] => 35 )
        [34] => Array ( [currency] => USD [countryid] => 36 )
        [35] => Array ( [currency] => USD [countryid] => 37 )
        [36] => Array ( [currency] => USD [countryid] => 40 )
        [37] => Array ( [currency] => USD [countryid] => 42 )
        [38] => Array ( [currency] => USD [countryid] => 45 )
        [39] => Array ( [currency] => USD [countryid] => 47 )
        [40] => Array ( [currency] => USD [countryid] => 48 )
        [41] => Array ( [currency] => USD [countryid] => 49 )
        [42] => Array ( [currency] => USD [countryid] => 50 )
        [43] => Array ( [currency] => USD [countryid] => 51 )
        [44] => Array ( [currency] => USD [countryid] => 53 )
        [45] => Array ( [currency] => USD [countryid] => 54 )
        [46] => Array ( [currency] => USD [countryid] => 55 )
        [47] => Array ( [currency] => USD [countryid] => 56 )
        [48] => Array ( [currency] => USD [countryid] => 57 )
        [49] => Array ( [currency] => USD [countryid] => 59 )
        [50] => Array ( [currency] => USD [countryid] => 61 )
        [51] => Array ( [currency] => USD [countryid] => 62 )
        [52] => Array ( [currency] => USD [countryid] => 63 )
        [53] => Array ( [currency] => USD [countryid] => 64 )
        [54] => Array ( [currency] => USD [countryid] => 65 )
        [55] => Array ( [currency] => USD [countryid] => 66 )
        [56] => Array ( [currency] => USD [countryid] => 67 )
        [57] => Array ( [currency] => USD [countryid] => 69 )
        [58] => Array ( [currency] => USD [countryid] => 70 )
        [59] => Array ( [currency] => USD [countryid] => 71 )
        [60] => Array ( [currency] => USD [countryid] => 74 )
        [61] => Array ( [currency] => USD [countryid] => 75 )
        [62] => Array ( [currency] => USD [countryid] => 76 )
        [63] => Array ( [currency] => USD [countryid] => 77 )
        [64] => Array ( [currency] => USD [countryid] => 78 )
        [65] => Array ( [currency] => USD [countryid] => 80 )
        [66] => Array ( [currency] => USD [countryid] => 82 )
        [67] => Array ( [currency] => USD [countryid] => 83 )
        [68] => Array ( [currency] => USD [countryid] => 84 )
        [69] => Array ( [currency] => USD [countryid] => 85 )
        [70] => Array ( [currency] => USD [countryid] => 86 )
        [71] => Array ( [currency] => USD [countryid] => 90 )
        [72] => Array ( [currency] => USD [countryid] => 91 )
        [73] => Array ( [currency] => USD [countryid] => 92 )
        [74] => Array ( [currency] => USD [countryid] => 93 )
        [75] => Array ( [currency] => USD [countryid] => 94 )
        [76] => Array ( [currency] => USD [countryid] => 95 )
        [77] => Array ( [currency] => USD [countryid] => 96 )
        [78] => Array ( [currency] => USD [countryid] => 97 )
        [79] => Array ( [currency] => USD [countryid] => 98 )
        [80] => Array ( [currency] => USD [countryid] => 99 )
        [81] => Array ( [currency] => USD [countryid] => 101 )
        [82] => Array ( [currency] => USD [countryid] => 104 )
        [83] => Array ( [currency] => USD [countryid] => 105 )
        [84] => Array ( [currency] => USD [countryid] => 107 )
        [85] => Array ( [currency] => USD [countryid] => 108 )
        [86] => Array ( [currency] => USD [countryid] => 110 )
        [87] => Array ( [currency] => USD [countryid] => 111 )
        [88] => Array ( [currency] => USD [countryid] => 114 )
        [89] => Array ( [currency] => USD [countryid] => 114 )
        [90] => Array ( [currency] => USD [countryid] => 115 )
        [91] => Array ( [currency] => USD [countryid] => 116 )
        [92] => Array ( [currency] => USD [countryid] => 118 )
        [93] => Array ( [currency] => USD [countryid] => 119 )
        [94] => Array ( [currency] => USD [countryid] => 122 )
        [95] => Array ( [currency] => USD [countryid] => 123 )
        [96] => Array ( [currency] => USD [countryid] => 124 )
        [97] => Array ( [currency] => USD [countryid] => 125 )
        [98] => Array ( [currency] => USD [countryid] => 126 )
        [99] => Array ( [currency] => USD [countryid] => 127 )
        [100] => Array ( [currency] => USD [countryid] => 128 )
        [101] => Array ( [currency] => USD [countryid] => 129 )
        [102] => Array ( [currency] => USD [countryid] => 130 )
        [103] => Array ( [currency] => USD [countryid] => 133 )
        [104] => Array ( [currency] => USD [countryid] => 134 )
        [105] => Array ( [currency] => USD [countryid] => 135 )
        [106] => Array ( [currency] => USD [countryid] => 137 )
        [107] => Array ( [currency] => USD [countryid] => 138 )
        [108] => Array ( [currency] => USD [countryid] => )
        [109] => Array ( [currency] => USD [countryid] => 6 )
        [110] => Array ( [currency] => EUR [countryid] => 13 )
        [111] => Array ( [currency] => EUR [countryid] => 18 )
        [112] => Array ( [currency] => EUR [countryid] => 32 )
        [113] => Array ( [currency] => EUR [countryid] => 33 )
        [114] => Array ( [currency] => EUR [countryid] => 39 )
        [115] => Array ( [currency] => EUR [countryid] => 41 )
        [116] => Array ( [currency] => EUR [countryid] => 42 )
        [117] => Array ( [currency] => EUR [countryid] => 44 )
        [118] => Array ( [currency] => EUR [countryid] => 46 )
        [119] => Array ( [currency] => EUR [countryid] => 52 )
        [120] => Array ( [currency] => EUR [countryid] => 58 )
        [121] => Array ( [currency] => EUR [countryid] => 60 )
        [122] => Array ( [currency] => EUR [countryid] => 72 )
        [123] => Array ( [currency] => EUR [countryid] => 73 )
        [124] => Array ( [currency] => EUR [countryid] => 79 )
        [125] => Array ( [currency] => EUR [countryid] => 88 )
        [126] => Array ( [currency] => EUR [countryid] => 102 )
        [127] => Array ( [currency] => EUR [countryid] => 103 )
        [128] => Array ( [currency] => EUR [countryid] => 106 )
        [129] => Array ( [currency] => EUR [countryid] => 112 )
        [130] => Array ( [currency] => EUR [countryid] => 113 )
        [131] => Array ( [currency] => EUR [countryid] => 117 )
        [132] => Array ( [currency] => EUR [countryid] => 120 )
        [133] => Array ( [currency] => EUR [countryid] => 121 )
        [134] => Array ( [currency] => EUR [countryid] => )
        [135] => Array ( [currency] => EUR [countryid] => 17 )
        [136] => Array ( [currency] => BRL [countryid] => )
        [137] => Array ( [currency] => BRL [countryid] => 81 )
        [138] => Array ( [currency] => MXN [countryid] => )
        [139] => Array ( [currency] => MXN [countryid] => 100 )
        [140] => Array ( [currency] => PEN [countryid] => )
        [141] => Array ( [currency] => PEN )
      )

我希望能够使用countryID查找密钥/ ID,以便我可以发现货币是什么。

我试过了:

echo array_search($countryID,$countryArray);

不起作用。

2 个答案:

答案 0 :(得分:2)

您可以使用array_column并执行此操作:

$array = array(
    array(
        'currency' => 'USD',
        'countryid' => 22,
    ),
    array(
        'currency' => 'GBP',
        'countryid' => 109,
    ),
    array(
        'countryid' => 999,
    ),
);

// Get the array column 'currency' indexed by 'id'
$countries = array_column($array, 'currency',  'countryid');
var_dump($countries);

// Returns this array:
// array(2) {
//     [22] => string(3) "USD"
//     [109] => string(3) "GBP"
// }

// Now 'search' for 22
var_dump($countries[22]);
// Returns 'string(3) "USD"'

如果您的PHP版本不是> = 5.5.0,您可以使用此处的userland实现:https://github.com/ramsey/array_column

答案 1 :(得分:0)

为简化起见,您可以按国家/地区ID索引数组。喜欢:

$countries= Array([countryId] => Array([currency] => GBP, [country] => GB), Array(...), ...); 

这样你只需要做

$country = $countries[$countryId];

但是,如果您无法控制阵列的结构,则PHP multidimensional array search by value已经回答了这个问题。 以下是该页面上答案的略微修改副本:

function searchForKey($country_id, $array) {
   foreach ($array as $key => $country) {
       if ($country['countryid'] === $country_id) {
           return $key;
       }
   }
   return null;
}

这会奏效。你应该这样称呼它:

$id = searchForKey('4434', $countries);
$country = $countries[$id];

重要的是要知道如果你使用===运算符比较类型必须完全相同,在这个例子中你必须搜索字符串或只使用==而不是===。