i have a csv file which i want to read via php in an array i think the command is $f=file($filename) now i want to edit this $f array calculate the number of column access it as $f[row][column] how do i do all this
Example: <?php $filename = "csvDemo.txt"; // Read the file $list = array_map("trim", file($filename)); // Split columns $list = array_map( create_function('$line', 'return split(",", $line);'), $list ); // Which column and row do I want to look at? $column = 3; $row = 2; echo $list[$column-1][$row-1]; ?> PHP: csvDemo.txt contains: one,two,three four,five,six seven,eight,nine Code (markup):
When you say table do you mean <table><tr><td></td></tr></table> or are you still talking about the CSV table?
<?php $filename = "tableDemo.html"; $contents = file_get_contents($filename); function matchTag($tagName, $contents) { if (preg_match_all('|<' . preg_quote($tagName) . '[^>]*>(.*?)</' . preg_quote($tagName) . '[^>]*>|si', $contents, $matches)) { return $matches[1]; } } $grid = array(); // Match a table if ($tables = matchTag("table", $contents)) { // Match only the first table $table = reset($tables); // Match rows if ($rows = matchTag("tr", $table)) { foreach ($rows as $rowNumber => $row) { // Match columns if ($cols = matchTag("td", $row)) { $grid[$rowNumber] = $cols; } } } } echo "<xmp>"; print_r($grid); echo "</xmp>"; ?> PHP:
<?php $row = 1; $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); ?>