我有以下设置,我试图使用数组结构数组。一旦在数组数组中找到值,我不确定如何获取键值。
$testboat = 'smallest boat';
$allboats = array(40=>array(1=>'big boat',
2=>'bigger boat'
),
30=>array(1=>'little boat',
2=>'tiny boat',
3=>'smallest boat'));
foreach($allboats as $boats){
foreach($boats as $boat){
if($testboat == $boat) {
/*looking to echo the key or value 30; */
}
}
}
答案 0 :(得分:1)
使用$key => $value
的{{1}}语法。此外,无需遍历内部数组:
foreach()
如果你想获得外键和内键:
foreach($allboats as $key => $boats){
if(in_array($testboat, $boats)) {
echo $key;
break; //if you want to stop after found
}
}
答案 1 :(得分:0)
$testboat = 'smallest boat';
$allboats = array(40=>array(1=>'big boat',
2=>'bigger boat'
),
30=>array(1=>'little boat',
2=>'tiny boat',
3=>'smallest boat'));
foreach($allboats as $id => $boats){
//$id will be 40, then 30
foreach($boats as $id2 => $boat){
//$id2 will be 1,2...
if($testboat == $boat) {
echo $id . '-' . $id2;
/*looking to echo the key or value 30; */
}
}
}
答案 2 :(得分:0)
您必须执行以下操作:
foreach($allboats as key1 => $boats){
foreach($boats as key2 => $boat){