我正在尝试使用 foreach 循环重新构造该数组:
Array
(
[0] => Array
(
[ID] => 0
[NAME] => 400
[QUANTITY] => 12
)
[1] => Array
(
[ID] => 0
[NAME] => 403
[QUANTITY] => 108
)
[2] => Array
(
[ID] => 5
[NAME] => 403
[QUANTITY] => 108
)
)
这就是我希望它看起来的样子:
Array
(
[ID] => Array
(
[NAME] => QUANTITY
[NAME] => QUANTITY
)
Array
(
[ID] => Array
(
[NAME] => QUANTITY
)
我想出的代码不能正常工作,数组似乎在每个循环上都被覆盖,只显示最后一个条目:
Array
(
[ID] => Array
(
[NAME] => QUANTITY
)
[ID] => Array
(
[NAME] => QUANTITY
)
)
这是我的代码:
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $key=>$value){
$i = $value["ID"];
$x = $value["NAME"];
$y = $value["QUANTITY"];
$arr[$i] = array( $x=>$y);
}