2012-11-15 4 views
0

PHP 프로그래밍에 익숙하지만 얼마 동안이 코드가 붙어 있습니다. 줄 단위로 .csv 파일을 읽은 다음 해당 값을 배열 목록에 저장하고 싶습니다..csv 파일을 읽고 배열 값에 값을 저장하십시오.

$file = fopen('Sub-Companies.csv', 'r');  
while (($line = 
fgetcsv($file)) !== FALSE) { 
print_r($line); 
list($customer_id[],$company_name[],$department[],$employee[],$country[],$zipcode[],$address[],$city[], 
$smth1[], $smth2[], $phone_no1[],$phone_no2[],$email[],$website[], 
$customer_no[],$problem1[],$problem2[]) = explode(";",$line); } 
fclose($file); var_dump($customer_id); 

문제는 파일을 올바르게 읽었지만 폭발이 작동하지 않고 배열이 null로 나타나는 문제입니다.

내가 고려하고있는 한 가지는 일부 배열에 ";" 왜냐하면이 배열의 값을 저장하기 위해 $ problem1과 $ problem2 배열을 가지고 있기 때문입니다.

도움이 될 것입니다.

+0

1.) CSV 파일의 예를 게시하십시오. 2) 나는 10 개 이상의 매개 변수를 가진'list()'를 사용하여 그런 코드를 본 적이 없다 !! – ComFreek

+0

예 : 601C6E90-DC11-41B1-B121-30859C532046 ;; NULL; NULL; DE; 19073; NULL; Wittenförden; NULL; NULL; 0004004; 839828298; [email protected]; NULL; ibd21 제가 너무 많은 매개 변수를 가지고있는 것이 문제라고 생각합니까? –

+0

도움이된다면이 코드를 사용해 보셨을 것입니다 :'$ string1 = "his; is; nw; house"; $ testarray = 폭발 (";", $ 문자열); print_r ($ testarray); '작동하지만 이걸 사용할 때'$ testarray = explode (";", $ line); print_r ($ testarray);'그렇지 않습니다. –

답변

2

잘못된 방법으로 fgetcsv()을 사용 중입니다.

chatting here on StackOverflow 동안 우리는이 해결책을 찾았습니다.

<?php 
// Create file data.csv with your data 
$handle = fopen('Sub-Companies.csv', 'r'); 

$customer_id = array(); 
$xyz_array = array(); 
// ... 

// Better use a specified length (second parameter) instead of 0 
// It slows down the whole process of reading the data! 
while (($line = fgetcsv($handle, 0, ';')) !== FALSE) { 
    $customer_id[] = $line[0]; 
    $xyz_array[] = $line[1]; 
} 
관련 문제