Php 使用可选键将查询结果排序到关联数组
使用PHP,我在数据库上运行一个查询,结果得到一个带有可选值的数据集。例如:Php 使用可选键将查询结果排序到关联数组,php,sorting,associative-array,Php,Sorting,Associative Array,使用PHP,我在数据库上运行一个查询,结果得到一个带有可选值的数据集。例如: Result: Array ( [0] => Array ( [attribute_1] => Red, [attribute_2] => Car, [name] => Sportscar ) [1] => Array ( [attribute_1] =
Result:
Array
(
[0] => Array
(
[attribute_1] => Red,
[attribute_2] => Car,
[name] => Sportscar
)
[1] => Array
(
[attribute_1] => Red,
[attribute_2] => ,
[name] => Rose
)
)
我希望将生成的数据格式化为:
Array
(
[Red] => Array
(
[Car] => Array
(
[0] => Sportscar
)
)
[0] => Rose
)
这样我就可以在
$array['Red']['Car'][0];
玫瑰在旁边
$array['Red'][0]
这将使使用一个漂亮的foreach循环输出数据变得更容易
当然,实际情况有点复杂,有很多值等等,但是这个例子演示了这个原理
问题是我想不出一种有效格式化数据的正确方法。不一定是递归的,但如果和elses是好的,至少不是一百万
有什么想法吗