Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create 2 dimensional arrays using a FOR loop in PHP?
    primarykey
    data
    text
    <p>Here's the array to store the value from the DB:</p> <pre><code>$dataTitle[0] = "TIR"; $dataTitle[1] = "OIL"; $dataTitle[2] = "SPK"; $dataDesc[0] = "Tires"; $dataDesc[1] = "Oil"; $dataDesc[2] = "Spark Plugs"; $dataValue[0] = "100"; $dataValue[1] = "10"; $dataValue[2] = "4"; </code></pre> <p>I can use the following to insert the data into 2D array manually but it doesn't serve purpose if i have 100 or more row records to be inserted. That's why FOR loop is needed for the below.</p> <pre><code>$ResultView = array(array($dataTitle[0], $dataDesc[0], $dataValue[0]), array($dataTitle[1], $dataDesc[1], $dataValue[1]), array($dataTitle[2], $dataDesc[2], $dataValue[2]) ); </code></pre> <p>If am using the following FOR LOOP, the 2D array only stored the last row record and omitted the 1st and 2nd row records.</p> <pre><code>for ($i=0; $i&lt;=2; $i++) { $ResultView = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i]) ); } </code></pre> <p>When I issue the output script below for the for loop above, I get the output value for the 3rd ROW and missing 1st and 2nd Row result. Please help!</p> <pre><code>for ($row=0; $row&lt;=2; $row++) { for ($col=0; $col&lt;=2; $col++) { echo $ResultView[$row][$col]."&amp;nbsp | "; } echo '&lt;br /&gt;'; } </code></pre> <p>But, I am looking a way the use FOR Loop instead for the above. How?</p> <p>The actual outputs are:</p> <pre><code>Row 1 =&gt; TIR | Tires | 100 Row 2 =&gt; OIL | Oil | 10 Row 3 =&gt; SPK | Spark Plugs | 4 </code></pre> <p>Please advice.</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.
 

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