Reconstructing associative arrays in php
231
I'm trying to recunstruct this array with a foreach loop :
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
)
)
This is what i want it to look like:
Array
(
[ID] => Array
(
[NAME] => QUANTITY
[NAME] => QUANTITY
)
Array
(
[ID] => Array
(
[NAME] => QUANTITY
)
The code i came up with does not work properly, the array seems to be overwritten on every loop displaying only the last entry:
Array
(
[ID] => Array
(
[NAME] => QUANTITY
)
[ID] => Array
(
[NAME] => QUANTITY
)
)
This is my code:
$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);
}
Answer
Solution:
Answer
Solution:
you can't have the same key multiple times. it's imposible. an array must have a unique key. you can create an array for the name key and it will look like this:
Answer
Solution:
As Vlad says you can't use the same index name...
But probably you want something like this:
Answer
Solution:
Try this code:
Answer
Solution:
I guess this would help. Considering you declare your array.
And you declare a new array with no value nor construction.
Now, try this code!