如何 json 使用 PHP 编码特定键值?

How to json encode Specific Key value using PHP?

我试过这段代码但收到错误。

$queryfetch = 'select * from table';
    
$result = mysqli_query($connection, $queryfetch);
        
$row = mysqli_fetch_assoc($result);
        
$data = [];
foreach($row as $key) {
   $data[] = [
       'first_name' => $key['first_name'],
       'last_name' => $key['last_name']
    ];
}
        
echo json_encode($data);

如何 json 使用 php 对特定键进行编码?

我已经解决了..

$queryfetch = 'select * from table';
    
$result = mysqli_query($connection, $queryfetch);
        
$row = mysqli_fetch_assoc($result);

$data[] = array(
    'first_name' => $row['first_name'],
    'last_name' => $row['last_name']
);

echo json_encode($data);

此代码运行良好..