这是我的PHP代码。这将从MySQL数据库中获取数据并存储在数组中。
所以,最初我有这个数组
$ array_init;
的print_r($ array_init);
Array (
[0] => Array ( [id] => 2 [class_name] => Jr KG )
[1] => Array ( [id] => 3 [class_name] => Sr KG )
[2] => Array ( [id] => 4 [class_name] => I )
[3] => Array ( [id] => 5 [class_name] => II )
[4] => Array ( [id] => 6 [class_name] => III )
[5] => Array ( [id] => 7 [class_name] => IV )
[6] => Array ( [id] => 8 [class_name] => V )
[7] => Array ( [id] => 9 [class_name] => VI )
[8] => Array ( [id] => 10 [class_name] => VII )
[9] => Array ( [id] => 11 [class_name] => VIII )
[10] => Array ( [id] => 12 [class_name] => IX )
[11] => Array ( [id] => 13 [class_name] => X )
)
我想在编码时进行快速遍历
所以更快的编码
我想创建一个像这样的关联数组:
Array (
[2] => Jr KG )
[3] => Sr KG )
[4] => I )
[5] => II )
[6] => III )
[7] => IV )
[8] => V )
[9] => VI )
[10] => VII )
[11] => VIII )
[12] => IX )
[13] => X )
)
我试过这个
foreach ($array_init as $row_class) {
$classes[] = [$row_class['id']=>$row_class['class_name']];
}
我得到了这个
Array ( [0] => Array (
[2] => Jr KG )
[1] => Array ( [3] => Sr KG )
[2] => Array ( [4] => I )
[3] => Array ( [5] => II )
[4] => Array ( [6] => III )
[5] => Array ( [7] => IV )
[6] => Array ( [8] => V )
[7] => Array ( [9] => VI )
[8] => Array ( [10] => VII )
[9] => Array ( [11] => VIII )
[10] => Array ( [12] => IX )
[11] => Array ( [13] => X )
)
顺便说一下,由于问题和解决方案都不同,所以并不重复。