0

The Objective

To read the csv file, and separate each line into an array. The first line (field names) displayed once, and then loop through the remaining data.

I have this function to open and explode the csv file

$myFile = "csv.csv";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$csv = explode(",", $theData);

This is the CSV file in question

id,sub,type,regprice,natprice
1,4,Team,40,75
2,4,Individual,15,35
3,4,Stunt Group,50,150
4,4,Coed Partner Stunt,50,150

What i need to know how to do, is load the first line into an array separately, then loop through the remaining arrays in the following manner.

Array[0][0] - Array[0][1] - Array[0][2] - Array[0][3] - Array[0][4]
-------------------------------------------------------------------
Array[1][0] - Array[1][1] - Array[1][2] - Array[1][3] - Array[1][4]
Array[2][0] - Array[2][1] - Array[2][2] - Array[2][3] - Array[2][4]
Array[3][0] - Array[3][1] - Array[3][2] - Array[3][3] - Array[3][4]
Array[4][0] - Array[4][1] - Array[4][2] - Array[4][3] - Array[4][4]

5 Answers 5

6

fgetcsv() will probably do all this for you.

Sign up to request clarification or add additional context in comments.

2 Comments

@eyze fgetcsv() takes a resource handle from fopen(). file() is a solid alternative if you REALLY need to explode the csv columns yourself as it will return an array strings representing each line.
you can mix file() with str_getcsv() :)
2

Try this:

foreach ($csv as $i=>$row) {
  $rowStr = implode(' - ',$row)."\n";
  print($rowStr);
  if ($i == 0) {
    print(str_repeat('-',strlen($rowStr))."\n");
  }
}

Edit: fixed syntax error.

Comments

1
<?php
$myFile = "csv.csv";
$fh = fopen($myFile, 'r');
$headers = fgetcsv($fh);
$data = array();
while (! feof($fh))
{
    $row = fgetcsv($fh);
    if (!empty($row))
    {
        $obj = new stdClass;
        foreach ($row as $i => $value)
        {
            $key = $headers[$i];
            $obj->$key = $value;
        }
        $data[] = $obj;
    }
}
fclose($fh);
print_r($data);
?>

This will output:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [sub] => 4
            [type] => Team
            [regprice] => 40
            [natprice] => 75
        )

    [1] => stdClass Object
        (
            [id] => 2
            [sub] => 4
            [type] => Individual
            [regprice] => 15
            [natprice] => 35
        )

    [2] => stdClass Object
        (
            [id] => 3
            [sub] => 4
            [type] => Stunt Group
            [regprice] => 50
            [natprice] => 150
        )

    [3] => stdClass Object
        (
            [id] => 4
            [sub] => 4
            [type] => Coed Partner Stunt
            [regprice] => 50
            [natprice] => 150
        )

)

Comments

0

I found this to work

I added a / at the end of the CSV lines.

$myFile = "csv.csv";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$csvpre = explode("/", $theData);
$i = 1;
    foreach ( $csvpre AS $key => $value){
        $info = explode(",", $value);    
            if($i == "1"){
                echo "$info[0] - $info[1] - $info[2] - $info[3] - $info[4]<br>";
                $i++;
                } else {
                echo "<span style=\"color:#ff0000;\">$info[0] - $info[1] - $info[2] - $info[3] - $info[4]</span><br>";
            }       
    }

Comments

0
$file_array=file('csv.csv');
$lines=count($file_array);

$first_line=explode(',',$file_array[0]);
$fl_text=implode(' - ',$first_line);    
echo $fl_text.'<br>';

for($i=1;$i<$lines;$i++)
   {
   $line_text=str_replace(',',' - ',$file_array[$i]);
   echo '<span style="color:#ff0000;">'.$line_text.'</span><br>';
   }

This includes a couple of ways to print out the lines without needing to explode each one. You can replace the ',' with a space using str_replace, or you can explode and then implode. Probably str_replace is more efficient.

Also, the file() command reads each line of a file into values in an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.