php - 如何更改结果集中的列名称并使用修改后的列名称在 PHP 中创建新结果集

标签 php names

示例:我当前的结果集:

array(7) {[0]=>array(2) 

{ ["class_id"]=>string(1) "1"["class"]=>string(3)"1st"}

{ ["class_id"]=>string(1) "2"["class"]=>string(3)"2nd"}

{ ["class_id"]=>string(1) "3"["class"]=>string(3)"3rd"}

我想要一个新的结果集:

array(7) {[0]=>array(2) 

{ ["new_id"]=>string(1) "1"["new_class"]=>string(3)"1st"}

{ ["new_id"]=>string(1) "2"["new_class"]=>string(3)"2nd"}

{ ["new_id"]=>string(1) "3"["new_class"]=>string(3)"3rd"}

我不希望这影响我数据库中的列名称。仅结果集。

最佳答案

向我们展示您的查询。例如,如果您正在执行以下查询:

SELECT class_id, class FROM table;

将其更改为:

SELECT class_id AS new_id, class AS new_class FROM table;

在查询中更改它无疑是最好的方法,因为您不必在 PHP 中做任何额外的工作,但是当然您也可以在 PHP 中修改它们。

// where $resultset is your original results..
foreach ($resultset as &$result) {
    $result_ = array('new_id' => $result['class_id'], 'new_class' => $result['class']);
    $result = $result_;
}

请注意,这些方法都不会影响您的数据库列。唯一的方法是通过 ALTER|MODIFY TABLE 语句。

关于php - 如何更改结果集中的列名称并使用修改后的列名称在 PHP 中创建新结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772658/

相关文章:

PHP - 电子邮件验证

javascript - 更新查询中的值作为 0 或空值输入表中

perl:如何从编号序列中创建紧凑的名称

r - 使用重复名称更改 R 向量中的多个元素

c# - DataGridView 编辑列名

php - 修改搜索功能以排除帖子并将结果限制为仅在前端每页 10 个

php DOMDocument 添加带有 DOCTYPE 声明的 <html> header

python - 获取 python numpy ndarray 的列名

r - R 中长表达式的简写名称

php - 那里有任何 PHP 人工智能项目吗?