Using php to read csv data and save it to an array

Source: Internet
Author: User
This article mainly introduces how php reads csv data and saves it to an array. This function is implemented through encapsulated class files, which is a practical technique for csv file operations, for more information about how to save csv data to an array, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Csv is a substitute for commonly used excel formats. in many cases, we export data into csv format, which is no different from excel, the following program is to read csv data and save it to an array. we need to operate on the data, so save it to the data. the code is as follows:

The code is as follows:

$ Info = csvtoarray: open('teste.csv ');
// Echo'

'; 
//print_r($info);
//echo '
';
Foreach ($ info as $ c)
{
Echo 'student ID: '. $ c [0];
Echo 'name: '. $ c [1];
Echo 'age: '. $ c [2];
Echo 'height: '. $ c [3].'
';
}


Final class csvtoarray {

/**
* Parse the csv file into an array and return it
*
* @ Param string $ file path of the csv file to be parsed
* @ Param char $ delimiter the default content delimiter in the csv file is;
* @ Return array
*/
Public static function open ($ file, $ delimiter = ';'){
Return self: ordenamultiarray (self: csvarray ($ file, $ delimiter), 1 );
}

Private function csvarray ($ file, $ delimiter)
{
$ Result = array ();
$ Size = filesize ($ file) + 1;
$ File = fopen ($ file, 'r ');
$ Keys = fgetcsv ($ file, $ size, $ delimiter );
Fseek ($ file, 0); // The original content does not exist here. add the content of the first line.
While ($ row = fgetcsv ($ file, $ size, $ delimiter ))
{
For ($ I = 0; $ I <count ($ row); $ I ++)
{
If (array_key_exists ($ I, $ keys ))
{
$ Row [$ keys [$ I] = $ row [$ I];
}
}
Print_r ($ row );
$ Result [] = $ row;
}

Fclose ($ file );

Return $ result;
}
Private function ordenamultiarray ($ multiarray, $ secondindex)
{
While (list ($ firstindex,) = each ($ multiarray ))
$ Indexmap [$ firstindex] = $ multiarray [$ firstindex] [$ secondindex];
Asort ($ indexmap );
While (list ($ firstindex,) = each ($ indexmap ))
If (is_numeric ($ firstindex ))
$ Sortedarray [] = $ multiarray [$ firstindex];
Else $ sortedarray [$ firstindex] = $ multiarray [$ firstindex];
Return $ sortedarray;
}
}

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.