MySQL / PHP:将整行推入关联数组,_without_命名每个单元格
16lz
2021-01-22
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.
我正在寻找一个编程快捷方式。我想将整行推入一个关联数组,$ key与列名相同,$ value是该特定列中的值,所有这一行,一次一行。
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 个解决方案
#1
1
Found it myself:
自己找到了:
The function that does this is $mysqli->fetch_array();
执行此操作的函数是$ mysqli-> fetch_array();
更多相关文章
- 在ajax中接收php多维数组。
- 查找具有特定数据字符串的数组并返回其具有的另一个数据字符串
- 使用PHP读取特定XML节点上特定数组的值
- 从数组中回显键和值
- Laravel将动态输入数据数组保存到数据库中
- 在多维数组中查找最小值并返回键
- 如何使用变量访问多维数组中的某些元素?
- 键入提示 - 指定对象数组
- 切出多维数组的一部分