foreach - PHP Loop to list all items in array, not just last -


i have bit of php code gets information csv file, stores in array, displays data in html table. code uses series of loops display both table headers (keys) , table contents (steps). problem having cannot work out how code loop through , list each array of information, @ moment shows last 1 (amount display specified $x variable in each loop.

if me more 1 single value using foreach loop in table appriciated, code follows:

<?php  // deals potential mac line endings ini_set("auto_detect_line_endings", true);   // define function read info .csv function readcsv($csvfile) {     $file_handle = fopen($csvfile, 'r');     while (!feof($file_handle) )          {         $line_of_text[] = fgetcsv($file_handle, 1024);         }     fclose($file_handle);     return $line_of_text;     }  // locate .csv read $csvfile = '500.csv'; $csv = readcsv($csvfile);  // uses first line of .csv keys $keys = $csv[0];  // loop set how many arrays print ($x=1; $x <=2; $x++)  {     $step = $csv[$x];     foreach ($step $k => $v)     {         $array[$keys[$k]] = $v;         }     echo '<pre>';     print_r($array);     echo '</pre>'; }  // use data arrays build html table print "<table>"; // header print "<tr>"; ($x=0; $x <=10; $x++) {     print "<th>$keys[$x]</th>"; } print "</tr>"; //field data print "<tr>"; ($x=0; $x <=10; $x++) {     print "<td>$step[$x]</td>"; } print "</tr>"; 

change $array[$keys[$k]] = $v; $array[$x][$keys[$k]] = $v; , use $array instead of $step in last for.

and keep small:

print "<table>"; print "<tr><th>".implode("</th><th>",$keys)."</th></tr>"; foreach ($array $arr){     print "<tr><td>".implode("</td><td>",$arr)."</td></tr>"; } print "</table>"; 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -