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 )
);
Notice that the key values represent the value of the id column in the table.
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;
}
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 ------
I have replaced
$array[] = $row;
with
$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 )
);
$array[$row['ID']] = $row;$array[$row['id']] = $row;mysql_*andmysqli_*aren't very different but again, not everyone is equally experienced otherwise we wouldn't need SO