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);                    
    }
840

Answer

Solution:

foreach($result as $key=>$value){
    $i = $value["ID"];
    $x = $value["NAME"];
    $y = $value["QUANTITY"]; 
    if(!isset($arr[$i])) $arr[$i] = array();
    $arr[$i][$x] = $y;
}
43

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:

[ID] => Array
    (
        [NAME] => Array 
               (
                    [0] => QUANTITY
                    [1] => QUANTITY
               )
    )
586

Answer

Solution:

As Vlad says you can't use the same index name...

But probably you want something like this:

foreach($result as $key=>$value){
    $i = $value["ID"];
    $x = $value["NAME"];
    $y = $value["QUANTITY"];

    // here you go:
    $data = array($x => $y);
    if (isset($arr[$i]) {
        $arr[$i] = array_merge($arr[$i], $data);
    } else {
        $arr[$i] = $data;
    }
}
83

Answer

Solution:

Try this code:

<?php

$newArray = array();

foreach ($result as $value)
{
    $newArray[$value['id']] = array($value['NAME']=>$value['QUANTITY']);
}
522

Answer

Solution:

I guess this would help. Considering you declare your array.

$array = 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)
);

And you declare a new array with no value nor construction.

$newArray = array();

Now, try this code!

foreach($array as $arr => $value)
{
    $id = $value["id"];
    $name = $value["Name"];
    $quantity = $value["quantity"];
    if(count($newArray[$id])>0){
        $newArray[$id] = array_merge($newArray[$id],array($name => $quantity));
    } else {
        $newArray[$id] = array($name => $quantity);
    }
}

People are also looking for solutions to the problem: php - facebook api server error 500 issue

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.