Jump to content

dynamically build multidimension array


siouxx

Recommended Posts

I'm having an issue with building a multidimensional array via a for (....)

the array should look like the example

any help would be much, much appreciated

thanks siouxx

 

array

["username"] = "steve"

array

["item"] = "12345"

array

"model" = "some model"

"quantity" = "36.2"

"details"

array

0 = "detail 1"

1 = "detail 2"

2 = "detail 3"

 

["username"] = "steve"

array

["item"] = "000257"

array

"model" = "some other model"

"quantity" = "77.8"

"details"

array

0 = "detail 1"

1 = "detail 2"

 

["username"] = "bob"

array

["item"] = "45678"

array

"model" = "other model"

"quantity" = "99.5"

"details"

array

0 = "detail 1"

1 = "detail 2"

2 = "detail 3"

3 = "detail 3"

 

the array return by sql looks like this

Array

(

    [0] => Array

        (

            [id_prod] => 6

            [id_tipo] => CPT

            [marca] => HP

            [modello] => Pavilion p6-200

            [prezzo] => 599.00

            [desc] => Computer da Tavolo

            [chiave] => 32

            [utente] => steve

            [qta] => 1

            [indata] => 2011-10-10 16:32:20

            [dettaglio] => HD 320GB - RAM 4GB - Scheda audio 5.1

        )

 

    [1] => Array

        (

            [id_prod] => 6

            [id_tipo] => CPT

            [marca] => HP

            [modello] => Pavilion p6-200

            [prezzo] => 599.00

            [desc] => Computer da Tavolo

            [chiave] => 31

            [utente] => steve

            [qta] => 1

            [indata] => 2011-10-10 16:32:20

            [dettaglio] => Intel Core i3-550 (3.20GHz, 4MB L3)

 

 

Link to comment
Share on other sites

self solved - I don't know if is efficient

 

$array=array();

$tot = count($dati);

$x = $prev = 0;

for ($i = 0; $i < $tot; $i++)

{

$id_prod = $dati[$i]["id_prod"];

$utente = $dati[$i]["utente"];

$prev != $dati[$i]["id_prod"] ? $x = 0 : "";

$array[$utente][$id_prod]["marca"] = $dati[$i]["marca"];

$array[$utente][$id_prod]["modello"] = $dati[$i]["modello"];

$array[$utente][$id_prod]["prezzo"] = $dati[$i]["prezzo"];

$array[$utente][$id_prod]["desc"] = $dati[$i]["desc"];

$array[$utente][$id_prod]["dettaglio"][$x++] = $dati[$i]["dettaglio"];

$prev = $dati[$i]["id_prod"];

}

return $array;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.