使用"id"的键值从MySQL生成PHP数组.柱子 [英] Generate PHP array from MySQL with key value from "id" column
问题描述
我有一个如下所示的MySQL数据库:
I have a MySQL database that looks like this:
ID TEXT PARENTID
20 Item1 null
23 Item2 20
27 Item3 20
80 Item4 27
我想要的是在这样的数组中检索该数据:
What I want is to retreive this data in an array like this:
Array (
[20] => Array ( [text] => Item1 [parentID] => )
[23] => Array ( [text] => Item2 [parentID] => 20 )
[27] => Array ( [text] => Item3 [parentID] => 20 )
[80] => Array ( [text] => Item4 [parentID] => 27 )
);
请注意,键值代表表中id列的值.
Notice that the key values represent the value of the id column in the table.
我已经尝试在mysql循环中建立数组(我仍在学习MySQLi,并且希望它会很快结束,但是这个示例在旧的MySQL中):
I have tried makin an array inside a mysql loop (I am still in the process of learning MySQLi, and hopefully it'll stick pretty soon but this example is in the old MySQL):
$hent_folder = mysql_query("SELECT id, text, parentID FROM folders");
while($row = mysql_fetch_assoc($hent_folder))
{
$array[] = $row;
}
这里的结果当然会生成自动"键值,而不是所需的ID列中的值,并且会将ID列作为内部数组的一部分.
The result here of course generates "automatic" key values and not the values from the ID column as wanted plus it puts the ID column as a part of the inside array.
在这里有成功的方法或解决方法吗?
Is there a way or workaround to succeed here?
我希望有人可以引导我朝正确的方向...
I hope someone can lead me in the right direction...
-----编辑------
----- EDIT ------
我已替换
$array[] = $row;
使用
$array[$row['ID']] = $row;
但是它仍然不会返回上面想要的结果.它返回以下内容:
But it still does not return the above wanted result. It returns this:
Array (
[0] => Array ( [id] => 20 [text] => Item1 [parentID] => )
[1] => Array ( [id] => 23 [text] => Item2 [parentID] => 20 )
[2] => Array ( [id] => 27 [text] => Item4 [parentID] => 20 )
[3] => Array ( [id] => 80 [text] => Item4 [parentID] => 27 )
);
推荐答案
这可作为关联数组来实现.
This is doable as an associative array.
您要做的就是改变
$array[] = $row
到
$array[$row['ID']] = $row
您需要确保数据库中的ID
列是唯一的,以便关联数组不会覆盖键(在这种情况下,仅保留具有重复ID的最后一条记录)
You need to make sure that the ID
column in your database is unique so that the associative array doesn't overwrite keys (in which case only the last record with duplicate ID's would remain)
关于"mysql循环"的问题-这只是一个普通的PHP while
循环,它循环遍历mysql_*
或mysqli_*
函数给出的记录.您正在遍历数据库中执行sql语句的返回结果,该语句仅以某种方式为您获取行并设置其格式.
Also about your 'mysql loop' - it's just a regular PHP while
loop that loops through records given by either mysql_*
or mysqli_*
functions. You're looping through the returned result from executing an sql statement on the database which just fetches and formats rows for you in a certain way.
MySQL和MySQL i 是两个不同的东西,但是它们使用的SQL并没有什么不同(也许准备好的语句除外).
学习SQL是您要执行的任务,并且要执行它,因为mysql_*
函数是
MySQL and MySQLi are two different things but the SQL they use is no different (except for maybe prepared statements?).
Learning SQL is what you're going to do and to execute it you're going to use mysqli_*
from now on since mysql_*
functions are deprecated
我只是想指出这些事情,以消除您可能已经或将来会遇到的困惑. :)
I just wanted to point those things out to clear out some confusion you might already have or will have in the future. :)
有关mysqli的更多信息,请阅读 php.net手册,如果您只是刚开始,那么一切都会有些复杂,但是那很好-了解您可以做到的事情,并努力了解您不能做到的事情.
For more reading on mysqli read the php.net manual, it's all a bit complex if you're just starting but that's fine - understand what you can and work to understand what you can't.
$row
数组中的索引为区分大小写,因此ID
和id
完全不同.一个会加总一个undefined index
错误.
the indexes in your $row
array are case-sensitive so ID
and id
are something different entirely. one will add up to an undefined index
error.
我在您的输出中注意到id
键实际上是小写,因此我尝试更改为该键.
I noticed in your output that the id
key is actually lowercase so I would try changing to that.
祝你好运!
这篇关于使用"id"的键值从MySQL生成PHP数组.柱子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!