我有列表或数组:
$var_menu_sub["1"]["1"] = "1|Sub Menu Label #1";
$var_menu_sub["1"]["2"] = "1|Sub Menu Label #2";
$var_menu_sub["1"]["3"] = "1|Sub Menu Label #3";
$var_menu_sub["2"]["this_key"] = "1|Change Password";
$var_menu_sub["3"]["1"] = "1|Sub Menu Label #1";
$var_menu_sub["3"]["2"] = "1|Sub Menu Label #2";
如果我只使用键“this_key”而不知道数组变量中的“2”键,那么如何获取值“Change Password”?
像这样(??? =平均动态):
$var_menu_sub["???"]["this_key"] = "1|Change Password";
答案 0 :(得分:2)
假设只有一个this_key
:
echo current(array_column($var_menu_sub, 'this_key'));
如果有多个this_key
,那么您只需要foreach
超过结果:
foreach(array_column($var_menu_sub, 'this_key') as $value) {
echo $value;
}
答案 1 :(得分:1)
一种方法是:
<?php
$var_menu_sub["1"]["1"] = "1|Sub Menu Label #1";
$var_menu_sub["1"]["2"] = "1|Sub Menu Label #2";
$var_menu_sub["1"]["3"] = "1|Sub Menu Label #3";
$var_menu_sub["2"]["this_key"] = "1|Change Password";
$var_menu_sub["3"]["1"] = "1|Sub Menu Label #1";
$var_menu_sub["3"]["2"] = "1|Sub Menu Label #2";
// Loop over entire array
foreach ($var_menu_sub as $data) {
//Loop over the array which is indexed by the key you know
foreach ($data as $key => $value) {
// Look for that key
if ($key == 'this_key') {
echo $value;
}
}
}