Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to format an associative array with another structure?
    primarykey
    data
    text
    <p>I have a query like this:</p> <pre><code>select truck, oil_type, km_min, km_max from trucks; </code></pre> <p>I'm using codeigniter and with the <code>$query-&gt;result_array()</code> function so its loaded in an associative array with this structure:</p> <pre><code>/* array( truck oil_type km_min km_max ) */ $arr[0]["truck"] = 2; $arr[0]["oil_type"] = 2; $arr[0]["km_min"] = 345; $arr[0]["km_max"] = 567; $arr[1]["truck"] = 2; $arr[1]["oil_type"] = 4; $arr[1]["km_min"] = 234; $arr[1]["km_max"] = 867; $arr[2]["truck"] = 1; $arr[2]["oil_type"] = 2; $arr[2]["km_min"] = 545; $arr[2]["km_max"] = 867; $arr[3]["truck"] = 4; $arr[3]["oil_type"] = 3; $arr[3]["km_min"] = 45; $arr[3]["km_max"] = 567; </code></pre> <p>Then, I'm trying to restructure it grouping the trucks by the id, something like this:</p> <pre><code>/* trucks - array( truck truck_data - array( oil_type km_min km_max ) ) */ $arr["truck"][0]= 2; $arr["truck"][0]["truck_data"][0]["oil_type"] = 2; $arr["truck"][0]["truck_data"][0]["km_min"] = 345; $arr["truck"][0]["truck_data"][0]["km_max"] = 567; $arr["truck"][0]["truck_data"][1]["oil_type"] = 4; $arr["truck"][0]["truck_data"][1]["km_min"] = 234; $arr["truck"][0]["truck_data"][1]["km_max"] = 867; $arr["truck"][1]= 1; $arr["truck"][1]["truck_data"][0]["oil_type"] = 2; $arr["truck"][1]["truck_data"][0]["km_min"] = 545; $arr["truck"][1]["truck_data"][0]["km_max"] = 867; $arr["truck"][2]= 4; $arr["truck"][2]["truck_data"][0]["oil_type"] = 3; $arr["truck"][2]["truck_data"][0]["km_min"] = 45; $arr["truck"][2]["truck_data"][0]["km_max"] = 567; </code></pre> <p>I thought in something like the code below:</p> <pre><code>$res = $query-&gt;result_array(); $cnt_total = count($res); $y = 0; for ($x=0; $x &lt; $cnt_total -1 ; $x++) { $truck = $res[$x]["truck"]; $trucks["truck"][$y] = $truck; $q = $x + 1; $i = 0; do{ $trucks[$y][$i]["oil_type"] = $res[$q]["oil_type"]; $trucks[$y][$i]["km_min"] = $res[$q]["km_max"]; $trucks[$y][$i]["km_max"] = $res[$q]["km_max"]; $q++; $i++; if ($q &lt;= $cnt_total) break; } while ( $truck === $res["truck"][$q] ); $y++; } </code></pre> <p>But does not work properly... I'm too far of the solution? The performance It's important for me in this particular case.</p> <p>There is a phpfiddle where you can try: <strong><a href="http://phpfiddle.org/main/code/67j-ui5" rel="nofollow">http://phpfiddle.org/main/code/67j-ui5</a></strong></p> <p>Any idea, tip, or advice will be appreciated, and if you need more info, let me know and I'll edit the post.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload