簡體   English   中英

獲取多維數組的關鍵

[英]Get key of multi dimensional 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);

不起作用。

你可以使用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

簡而言之,您可以按國家/地區ID索引數組。 喜歡:

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

這樣你只需要這樣做

$country = $countries[$countryId];

但是,如果您無法控制數組的結構,那么PHP問題的PHP多維數組搜索已經回答了這個問題。 以下是該頁面上答案的略微修改副本:

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

重要的是要知道如果你使用===運算符比較類型必須完全相同,在這個例子中你必須搜索字符串或只使用==而不是===。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM