使用特定名称在PHP中打印数组

时间:2017-09-05 06:08:48

标签: php arrays arraylist

我想在首页打印一个数组作为表格,但我不想显示姓氏。如何在PHP中编写代码来执行此操作?

?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

请帮忙

2 个答案:

答案 0 :(得分:1)

试试这个

<table>    
<?php
 foreach ($records as $key => $value) {   
    echo "<tr>";
    echo "<td>".$value['id']."</td>";
    echo "<td>".$value['first_name']."</td>";  
    echo "</tr>";

 }    
?>
</table>

这将以表格格式显示 id first_name

答案 1 :(得分:0)

<table>    
<?php
 foreach ($records as $value) {   
    echo "<tr>";
    echo "<td>".$value['id']."</td>";
    echo "<td>".$value['first_name']."</td>";  
    echo "</tr>";

 }    
?>
</table>