-4
$array = array('first' => array(1, 3), 'second' => array(5,6), 'third' => array(8,4));

foreach($array as $a){
  echo "key = " . key($a) . "\n";
};

如何从此数组中获取索引名称?我想收到:

key = first
key = second
key = third

但这在每种情况下都会返回我 key = 0。为什么?我怎样才能得到索引的名称?

键盘

4

2 回答 2

0

试试这个:

foreach($array as $key => $val){
  echo "key = " . $key . "\n";
};

如果您想打印该值:

foreach($array as $key => $val){
   echo "value = " . $val . "\n";
 };
于 2013-09-02T10:55:49.180 回答
0

使用foreach的其他语法:

foreach ($array as $key => $value)
{
    echo "Key: ".$key;
}
于 2013-09-02T10:55:49.387 回答