0

I have a CSV file which the code below uses to put the contents of the CSV into a multidimensional array.

<?php

$val = $_GET['articleid'];
//URL Pass STring

$arrCSV = array();
// Open the CSV
if (($handle = fopen("../articles/artlist.csv", "r")) !==FALSE) {                         
// Set the parent array key to 0
$key = 0;
// While there is data available loop through unlimited times (0) using separator (,)
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
   // Count the total keys in each row
   $c = count($data);
   //Populate the array
   for ($x=0;$x<$c;$x++) {
   $arrCSV[$key][$x] = $data[$x];
   }
   $key++;
} // end while
// Close the CSV file
fclose($handle);
} // end if

?>

My first part is to output the entire array.

I think this code below is wrong :(

  <?php       
        for ( $row = 1; $row <$arrCSV; $row++ )
            {
        echo ''.$arrCSV[$row]['6'].'';  
            }
    ?>

Then I would like it ONLY to output one of the columns [*] [6].

I think I should get here fist before I try the rest.

Any guidance would help.

I am trying to learn, so far I have got this far.

Thank you

2
  • Ok. I need this part.. <?php print_r($arrCSV); ?> Commented Jan 25, 2012 at 22:15
  • Hello, Can anyone help with the code please. Commented Jan 25, 2012 at 23:21

3 Answers 3

1

Does this not work?:

<?php
$val = $_GET['articleid'];
//URL Pass STring

$arrCSV = array();
// Open the CSV
if (($handle = fopen("../articles/artlist.csv", "r")) !==FALSE) {                         

// While there is data available loop through unlimited times (0) using separator (,)
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
   $arrCSV[] = $data;
} // end while
// Close the CSV file
fclose($handle);
} // end if

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

2 Comments

This works.. <?php print_r($arrCSV); ?> How can I only out put ONE of the columns.
Ok, this works. I have changed my code above. Now I need to know how to show ONLY the results based on a column content. e.g. show all the rows id column 6 has RED,
1

I use this function to convert CSV into an array

function csv2array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") 
{
    $array = array();
    $size = strlen($string);
    $columnIndex = 0;
    $rowIndex = 0;
    $fieldValue = "";
    $isEnclosured = False;

    for($i=0; $i<$size;$i++) 
    {
        $char = $string{$i};
        $addChar = "";

        if($isEnclosured) 
        {
            if($char == $enclosureChar) 
            {
                if($i+1<$size && $string{$i+1} == $enclosureChar)
                {
                    $addChar = $char;
                    $i++; 
                }
                else
                {
                    $isEnclosured = false;
                }
            }
            else 
            {
                $addChar=$char;
            }
        }
        else 
        {
            if($char==$enclosureChar) 
            {
                $isEnclosured = true;
            }
            else 
            {
                if($char==$separatorChar) 
                {
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue = "";
                    $columnIndex++;
                }
                elseif($char==$newlineChar) 
                {
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";
                    $columnIndex=0;
                    $rowIndex++;
                }
                else 
                {
                    $addChar=$char;
                }
            }
        }

        if($addChar != "")
        {
            $fieldValue.=$addChar;
        }
    }

    if($fieldValue) 
    {
        $array[$rowIndex][$columnIndex] = $fieldValue;
    }

    return $array;
}

You can simply use it like this:

<?php

$handle = fopen("../articles/artlist.csv", "r");
$arr = csv2array($handle);

print_r($arr);

?>

2 Comments

The code I have works, I just need to this part working now. ' $searchchar="cat"; for ( $row = 1; $row <sizeof($arrCSV); $row++ ) { { echo ''.$arrCSV[$row]['6'].''; } } ?>'
I can make it show on column, but I only want to show the rows based on another columns vale. e.g. RED. There are 23 RED's contained in on column, this it should who 23 line.
0

This is the solution

To access you CSV:

 <?php

$val = $_GET['articleid'];
//URL Pass STring

$arrCSV = array();
// Open the CSV
if (($handle = fopen("../articles/artlist.csv", "r")) !==FALSE) {                         
// Set the parent array key to 0
$key = 0;
// While there is data available loop through unlimited times (0) using separator (,)
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
   // Count the total keys in each row
   $c = count($data);
   //Populate the array
   for ($x=0;$x<$c;$x++) {
   $arrCSV[$key][$x] = $data[$x];
   }
   $key++;
} // end while
// Close the CSV file
fclose($handle);
} // end if

?>

To display on entire column in the CSV:

 <?php       
        for ( $row = 1; $row <$arrCSV; $row++ )
            {
        echo ''.$arrCSV[$row]['6'].'';  
            }
    ?>

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.