0

I have this function

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
    {
    echo "Not a valid file name";
        return FALSE;

     }
    $header = NULL;
    $data = array();



    $fname = file($filename);
    $a1 = array('username','fullname','email','dept');  

    if (($handle = fopen($filename, 'r')) !== FALSE)
    {       
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {    
            if(!$header)
            {
                 $header = $row; 

                // print_r(array_diff_key($header,$array)) ;
                }
            else { 
                $data[] = array_combine($header, $row);  
                } 
        }     
        fclose($handle);
    }

    return $data;

}

It return associative array. My csv file contains the header username fullname email dept but i require the header not to be mentione din the csv file how do i go about it

1
  • So you just need to remove the header part? Commented Sep 23, 2010 at 16:27

1 Answer 1

1

How about:

$seen_header = false; // initialize to false.
$data = array();

// keep reading from the file till you have lines.
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { 

        // $seen_header will be false for the fist time.
        if(!$seen_header) {
                // don't add header to $data and make $seen_header true.
                $seen_header = true;
        }else{   
                // non header..add it.
                $data[] = $row;
        }       
}     
Sign up to request clarification or add additional context in comments.

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.