doufangyan6862 2014-08-10 19:12
浏览 39
已采纳

MySQL / PHP:将整行推入关联数组,_without_命名每个单元格

I am looking for a programming shortcut. I want to push the entire row into an associative array, the $key being the same as the column name, and the $value being the value in that particular column, and all that, one row at a time.

So far, the only way I know how to do something like this is:

$array = [];
$push_item = [];

while ($row = $result->fetch_assoc())
{
     $column_name_1 = $row['column_name_1'];
     $column_name_2 = $row['column_name_2'];
     //etc.

   $push_item = ["column_name_1" => $column_name_1, "column_name_2" => $column_name_2, ];
   array_push($array, $push_item);
}

And like that, for every row in the table.

I am looking for a solution that will go easier on my typing. Can I grab the column names and data automatically?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongxiai3003 2014-08-10 19:17
    关注

    Found it myself:

    The function that does this is $mysqli->fetch_array();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图