使用PHP的网站作为参考我试图在关联数组中搜索已知值的键:
http://php.net/manual/en/function.array-search.php
以下代码始终返回1,但假设返回120:
$location_key = array_search(1, array_column($locations, 'main'));
的print_r($位置):
Array
(
[120] => Array
(
[clientid] => 122103
[name] => HQ
[address] => 2013 BENSON GARDEN BLVD
[address2] =>
[city] => OMAHA
[state] => NE
[zip] => 68134
[country] => UNITED STATES
[lat] => 00.000
[lng] => -0.0000
[taxrate] => 0
[main] => 1
[active] => 1
[contactid] => 14
)
[122] => Array
(
[clientid] => 122103
[name] => Branch
[address] => 515E E 72ND ST
[address2] =>
[city] => NEW YORK
[state] => NY
[zip] => 10021
[country] => UNITED STATES
[lat] => 40.766705
[lng] => -73.952965
[taxrate] => 0
[main] => 0
[active] => 1
[contactid] => 0
)
)
不确定为什么PHP的网站引用了最高投票和#34;用户贡献的注释"哪个不起作用。
答案 0 :(得分:1)
这是因为array_column()
没有保留您的密钥。因此,首先需要array_combine()
来自array_column()
的数组的密钥,例如
$location_key = array_search(1, array_combine(array_keys($locations), array_column($locations, 'main')));