如何在PHP中显示密钥数组的密钥?

时间:2013-08-04 17:48:43

标签: php arrays

这是$记录的内容 enter image description here

如果我想显示"键"这个关键数组,如何编码?

e.g。在第一轮foreach循环中我喜欢显示 1(键),1(id),10(价格)等。 我会使用$ record [' id'],$ record [' price'],但我不知道要编码以显示密钥的内容?

6 个答案:

答案 0 :(得分:3)

foreach ($records as $key => $record) {
    echo $key;
}

在PHP手册中查看foreach以获取更多信息:http://php.net/manual/en/control-structures.foreach.php

答案 1 :(得分:2)

foreach($records as $key=>$val){
   echo $key;
}

答案 2 :(得分:2)

foreach($records as $key => $record) {
  echo $key . '(key), ' . $record['id'] . '(id), ' . $record['price'] . '(price)';
}

答案 3 :(得分:1)

你试过使用foreach吗?

foreach($records as $key => $value) {
     echo "Current key: ".$key."<br />";
     print_r($value);
}

答案 4 :(得分:1)

foreach($records as $key=>$record){
  echo "key: ".$key."</br>";
  echo "ID: ".$record['id']."</br>";
  echo "Price: ".$record['price']."</br>";
}

答案 5 :(得分:1)

<?php
foreach ($records as $record) {
    foreach ($record as $key => $value) {
        echo $value . '(' . $key . ')';
    }
}
?>