如何回显子数组的key:value对?的PHP [英] How to echo the key:value pairs of a sub array? PHP
本文介绍了如何回显子数组的key:value对?的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在提取二维数组中仅一个子数组中的键值对时遇到麻烦.我正在尝试以以下格式获取它:
I'm having trouble pulling out the key value pairs in only one sub array of a two dimensional array. I'm trying to get it in the following format:
"Insect: b: beetle
Insect: m: moth
etc..."
这是到目前为止我得到的:
here is what I've got so far:
$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
推荐答案
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>";
}
这篇关于如何回显子数组的key:value对?的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文