我想這recunstruct陣列foreach循環關聯數組:重建在PHP
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);
}
它的工作就像一個魅力!感謝您的快速回復。 – Kwqubik