我在提取二维数组中仅一个子数组中的键值对时遇到麻烦。 我正在尝试以以下格式获取它:
"Insect: b: beetle
Insect: m: moth
etc..."
这是到目前为止我得到的:
$animals = array(
'insect' => array('b'=>"beetle", 'm'=>"moth", 's'=>"spider"),
'mammal' => array('d'=>"dolphin", 'h'=>"human", 'c'=>"chimp"),
'fish' => array('a'=>"angler", 'sh'=>"shark", 'p'=>"puffer"));
echo $animals['insect']; // trying to print sub array??
echo "<pre>";
foreach($animals as $Mkey => $domains)
foreach($domains as $key => $species)
echo "$Mkey: $key : $species<br>"; //prints whole array
答案 0 :(得分:2)
foreach($animals['insect'] as $Mkey => $species) {
echo "$Mkey : $species<br>";
}
// dynamic key:
$key = 'insect';
foreach($animals[$key] as $Mkey => $species) {
echo "$key: $Mkey : $species<br>";
}