siouxx Posted December 3, 2011 Share Posted December 3, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/252380-dynamically-build-multidimension-array/ Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 And your code? Quote Link to comment https://forums.phpfreaks.com/topic/252380-dynamically-build-multidimension-array/#findComment-1293885 Share on other sites More sharing options...
siouxx Posted December 3, 2011 Author Share Posted December 3, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/252380-dynamically-build-multidimension-array/#findComment-1293890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.