我正在试图弄清楚如何使用Mysqli从mysql数据库获取字段并将它们放入PHP数组...但我想使用其中一个字段作为多维数组的键分隔符。
例如,这是我在数据库中可能拥有的内容:
ID名称价格可用
1 8×10 12.00ÿ
2的5x7 13.00ÿ
3 11x14 14.00Ñ
如果我把它放到这样的数组中:
$OSQL = "SELECT * FROM Prices"; $PriceList=array();
$OSQL = mysqli_query($dbc,$OSQL);
while ($RW = mysqli_fetch_assoc($OSQL)) { $PriceList[]=$RW; }
我会得到这个:
Array (
[0] => Array
(
[id] => 1
[Name] => 8x10
[Price] => 12.00
[Active] => Y
)
[1] => Array
(
[id] => 2
[Name] => 5x7
[Price] => 13.00
[Active] => Y
)
[2] => Array
(
[id] => 3
[Name] => 11x14
[Price] => 14.00
[Active] => N
)
但我想像NAME一样将其格式化为多维数组键名而不是0,1和2:
Array (
[8x10] => Array
(
[id] => 1
[Name] => 8x10
[Price] => 12.00
[Active] => Y
)
[5x7] => Array
(
[id] => 2
[Name] => 5x7
[Price] => 13.00
[Active] => Y
)
[11x14] => Array
(
[id] => 3
[Name] => 11x14
[Price] => 14.00
[Active] => N
)
我意识到我总是可以使用while($ RW = mysqli_fetch_assoc(...))循环重新组织数组,但我很好奇是否在制作数组时有一个快速的解决方案,甚至是从数据库。是否有一种更简单的方式在我的鼻子前面,我错过了它?谢谢!