这是 $records 的内容
如果我想显示这个键数组的“键”,如何编码?
例如,在第一轮 foreach 循环中,我喜欢显示 1(键)、1(id)、10(价格)等等。我会使用 $record['id'], $record['price'],但我不知道要编写什么代码来显示密钥?
foreach ($records as $key => $record) {
echo $key;
}
查看 PHP 手册中的 foreach 以获取更多信息: http: //php.net/manual/en/control-structures.foreach.php
foreach($records as $key=>$val){
echo $key;
}
foreach($records as $key => $record) {
echo $key . '(key), ' . $record['id'] . '(id), ' . $record['price'] . '(price)';
}
您是否尝试过使用 foreach ?
foreach($records as $key => $value) {
echo "Current key: ".$key."<br />";
print_r($value);
}
foreach($records as $key=>$record){
echo "key: ".$key."</br>";
echo "ID: ".$record['id']."</br>";
echo "Price: ".$record['price']."</br>";
}
<?php
foreach ($records as $record) {
foreach ($record as $key => $value) {
echo $value . '(' . $key . ')';
}
}
?>