Php reads csv data and saves it to the array code

Source: Internet
Author: User

Php reads csv data and saves it to the array code. csv is a commonly used substitute for excel format. In many cases, we export data in 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 it is saved to the data.

The php tutorial reads csv data and saves it to the array code.
Csv is a commonly used substitute for excel format. In many cases, we export data to 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 it is saved to the data.

$ Info = csvtoarray: open('teste.csv ');
// Echo '<pre> ';
// Print_r ($ info );
// Echo '</pre> ';
Foreach ($ info as $ c)
{
Echo 'student ID: '. $ c [0];
Echo 'name: '. $ c [1];
Echo 'Age: '. $ c [2];
Echo 'height: '. $ c [3].' <br> ';
}
 

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;
}
}


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.