fgetcsv

PHP read in specific csv file column as an array

妖精的绣舞 提交于 2019-12-05 22:21:14
I am new to PHP and would like to be able to read in a csv file which has two columns, one is the number (kind of like a ID) then the other holds a integer value. I have looked up the fgetcsv function but I have not been able to find a way to read a specific column from the csv file. I would like to get all the values from the second column only, without the heading. Any way of doing this? This is what I have so far: $file = fopen('data.csv', 'r'); $line = fgetcsv($file); And this is some sample data from the csv file: ID,Value 1,243.00 2,243.00 3,243.00 4,243.00 5,123.11 6,243.00 7,180.00 8

Missing first character of fields in csv

偶尔善良 提交于 2019-12-05 09:44:34
I'm working on a csv import script in php. It works fine, except for foreign characters in the beginning of a field. The code looks like this if (($handle = fopen($filename, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) $teljing[] = $data; fclose($handle); } Here is a data example showing my issue føroyskir stavir, "Kr. 201,50" óvirkin ting, "Kr. 100,00" This will result in the following array ( [0] => array ( [0] => 'føroyskir stavir', [1] => 'Kr. 201,50' ) [1] => array ( [0] => 'virkin ting', <--- Should be 'óvirkin ting' [1] => 'Kr. 100,00' ) ) I have seen this

Get associative array from csv

♀尐吖头ヾ 提交于 2019-12-04 16:49:46
I open a csv file from a url. Each line has 4 fields and each field has a name: Field1;Field2;Field3;Field4 Now my script handles the csv data as one single line but I want to have it this way: Array ( [0] => array( ['field1'] => 1 ['field2'] => 2 ['field3'] => 3 ['field4'] => 4 ) ) Any ideas? Here is my code: if (($handle = fopen ( $eurl, "r" )) !== FALSE) { while ( ($data = fgetcsv ( $handle, 4096, ";" )) !== FALSE ) { $num = count ( $data ); for($c = 0; $c < $num; $c ++) { echo $data [$c]; } } fclose ( $handle ); } t.csv id;name;sex;age 1;xudong;m;23 2;jack;f;24 3;minjie;f;25 <?php $eurl =

How to get full filepath when uploading files in PHP?

与世无争的帅哥 提交于 2019-12-04 12:35:21
问题 I really want to know how am I gonna get the full filepath when I upload a file in PHP? Here's my my problem... I am importing a csv file in PHP. Uploading a file isn't a problem but the function used to import csv files which is fgetcsv() requires fopen. fopen is the one giving me a headache because it requires an exact filepath which means that the file should be in the same directory with the php file. What if the user gets a file from a different directory. Here's my codes: index.php :

PHP fgetcsv() - find number of columns

末鹿安然 提交于 2019-12-04 06:46:50
I'm trying to determine how many columns a csv file has. Here's my script, which is only utilizing the first column, but I'm running slightly blind. I want to place a variable that limits the column count. (since I may make a mistake and add a column, or even miss a column) <?php $allowedColNum=5; $batchcount=0; $file = fopen($file_name, "r"); while ($line = fgetcsv($file)){ /* I want to stop the loop if the $allowedColNum is not correct */ $col = $line[0]; echo $batchcount++.". ".$col."\n"; } fclose($file); ?> I'm sure it's one of those easy easy things that I'm not getting. If I understand,

How to AUTOINCREMENT starting from a certain number/offset?

只谈情不闲聊 提交于 2019-12-04 06:31:40
问题 I am running an fgetcsv query to import a bunch of data from a CSV into WordPress. I am wondering how I can start an auto increment from a certain number, for example, from 1000 onwards. $import1="INSERT into wp_postmeta (meta_id,post_id,meta_key,meta_value) values(',',',','first_name','$data[1]')"; This is an example of the code. the meta_id should use a normal auto increment, but the post_id I want to start from a certain number. How would I accomplish this? 回答1: To set the starting value

Import Large CSV file into MySQL

徘徊边缘 提交于 2019-12-03 09:13:58
I am trying to import a csv file into a mysql table and I currently have a script that is running line by line because I need to hash an id combined with another id as well as format the date for mysql format. The csv file has MORE columns than I am currently importing. Is it easier to just import all columns? I was reading about LOAD DATA INFILE (http://dev.mysql.com/doc/refman/5.1/en/load-data.html), but I am wondering how I can use this and hash the ids and format the date without doing row by row execution. My current script is taking way too long and causing site performance issues while

Using curl as an alternative to fopen file resource for fgetcsv

♀尐吖头ヾ 提交于 2019-12-03 07:28:23
Is it possible to make curl, access a url and the result as a file resource? like how fopen does it. My goals: Parse a CSV file Pass it to fgetcsv My obstruction: fopen is disabled My chunk of codes (in fopen) $url = "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=sl1d1t1n&e=.csv"; $f = fopen($url, 'r'); print_r(fgetcsv($f)); Then, I am trying this on curl. $curl = curl_init(); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $param); curl_setopt($curl,

Convert a string into list of arrays

一曲冷凌霜 提交于 2019-12-02 13:38:59
How do I convert a REQUEST string into arrays in a list like the following? $_REQUEST["InventoryData"] == sku=qty&234444=11&ShirtBig=111&ShirtSmall=101&empty=0 Array ( [0] => sku [1] => qty ) Array ( [0] => 234444 [1] => 11 ) Array ( [0] => ShirtBig [1] => 111 ) Array ( [0] => ShirtSmall [1] => 101 ) Array ( [0] => empty [1] => 0 ) This is a modification of "MASS UPDATE STOCK LEVELS IN MAGENTO – FAST" script for updating using a client side submission of data. $result = array(); parse_str($_REQUEST['InventoryData'], $data); foreach ($data as $key => $value) { $result[] = array($key, $value); }

How to specify encoding while process csv file in PHP?

感情迁移 提交于 2019-12-02 01:45:40
问题 <?php $row = 1; $handle = fopen ("test.csv","r"); while ($data = fgetcsv ($handle, 1000, ",")) { $num = count ($data); print "<p> $num fields in line $row: <br>\n"; $row++; for ($c=0; $c < $num; $c++) { print $data[$c] . "<br>\n"; } } fclose ($handle); ?> The above comes from php manual,but I didn't see where to specify the encoding(like utf8 or so) 回答1: Try to change the locale. Like it says below the example in the manual you gave: Note: Locale setting is taken into account by this function