2013-03-17 3 views
0

임이 데이터로 작업 :CSV 파일은 mulit 차원하기 위해 PHP 배열을

name,address_one,address_two,address_postcode 
Bob,1 A Street,A town,AA11AA 
Andy,92 Church St,Manchester,M20 3JN 
Sarah,893 London Road,Enfield,EN3 9HB 
Freda,67 Green Close,Newcastle,Nw5 2ED 

그것은 잘 형성 .CSV 파일입니다. 그것을 구문 분석하고 XML로 포맷하기 위해 다차원 PHP 배열로 변환 할 수 있어야합니다.

public function parseFile($fileIn) 
{ 

    if (($handle = fopen("$fileIn", "r")) !== FALSE) 
    { 

     $header = NULL; 
     while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) 
      { 
        if(!$header) 
      { 
       $header = $row; 
       foreach($header as $k => $v) 
       {     
        if($x = strpos("$v", '_')) 
        { 
         $child = substr("$v", $x+1); 
         array_push($header, $child); 
        } 
       } 
      } else { 
         $data[] = array_combine($header, $row); 
      } 
      } 

     print_r($data); 

      fclose($handle); 
    } else { 
     throw new Exception('Unable to open the file!'); 
    } 
} // parseFile 
: 나는 가까이이 코드는군요

<?xml version="1.0"?> 
<root> 
<name>Bob</name> 
<address> 
<one>1 A Street</one> 
<two>A town</two> 
<postcode>AA11AA</postcode> 
</address> 
<name>Andy</name> 
<address> 
<one>92 Chuch St</one> 
<two>Manchester</two> 
<postcode>M20 3JN</postcode> 
</address> 
...omitted 
</root> 

:처럼 그때 XML의가 someting이 변환 할

Array 
(
['name']=> Bob 
[0] => Array 
    (
     [address] => Array 
      (
       [one] => 1 A Street 
       [two] => A town 
       [postcode] => WC2 9GH 

      ) 
    ) 
['name']=> Andy 
[1] => Array 
    (
     [address] => Array 
      (
       [one] => 92 Chuch St 
       [two] => Manchester 
       [postcode] => M20 3JN 

      ) 
    ) 
... omitted 
) 

:처럼 내 배열이 보일 수 있습니다

일단 성공

하지만 내 매개 변수가 동기화되지 않은 것으로 보입니다.

Array 
(
[0] => 
[1] => 
[2] => 
[3] => 
) 

내가 가진 작은 되감기 경우 :

Warning: array_combine(): Both parameters should have an equal number of elements in 

var_dump($header); 

는 공개와 함께

$header = NULL; 
while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) 
{ 
if(!$header) $header = $row; 
else $data[] = array_combine($header, $row); 
} 
print_r($data); 

내가 얻을 :

Array 
(
[0] => Array 
    (
     [name] => Bob 
     [address_one] => 1 A Street 
     [address_two] => A town 
     [address_postcode] => AA11AA 
    ) 

[1] => Array 
    (
     [name] => Andy 
     [address_one] => 92 Church St 
     [address_two] => Manchester 
     [address_postcode] => M20 3JN 
    ) 

어느 쪽이 가깝지 만 .csv 파일의 머리글 ('_'문자열)을 부모 자식 노드 (XML이 아닌 배열이라고 생각할 수 있음)로 분할 할 수 있어야합니다. 하나는 주소의 자녀가 어디에

그래서 예를 들어, address_one는

Array 
(
[0] => Array 
    (
    [address] => Array 
       (
        [one] => 1 A Street 
     ) 
) 
) 

이 될 것입니다. 그리고 csv 파일의 다른 모든 제목에도 밑줄이 있습니다. 밑줄이없는 표제는 단순히 값으로 매핑됩니다.

도와 주시겠습니까?

답변

0

문제는 "$ header = $ row;"입니다. 나중에 헤더에 여분의 데이터를 추가합니다 (array_push ($ header, $ child)).

이 당신에게 다음과 같습니다 배열을 제공합니다 :이 데이터를 필터링하기 전에

array(
    [0] => name 
    [1] => address_one 
    [2] => address_two 
    [3] => address_postcode 
    [4] => one 
    [5] => two 
    [6] => postcode 
); 

당신은 기본 값을 설정하지 않아야합니다. 그래서 $ tmpHeaders = array();를 사용할 수 있습니다. 그런 다음 행에있는 데이터를 기반으로 채 웁니다. 이 같은

뭔가 :

$tmpHeaders = []; 
foreach ($row as $key => $value) { 
    if($x = strpos("$value", '_')) { 
     $value = substr("$value", $x + 1); 
    } 

    array_push($tmpHeaders, $value); 
} 
$header = $tmpHeaders; 

이 방법은 당신의 데이터 배열 및 헤더의 배열 요소의 동일한 금액을 포함한다. 이제 이름을 필터링하고 주소 필드를 배열 필드로 분리 할 수 ​​있습니다.I 필요할

+0

는 이와 같은 포맷 할' 배열 ( [ '이름'] => 밥 [0] => 배열 ( [주소] => 배열 ( [하나] => 1 A 거리 [두] =>를 도시 [우편 번호] => WC2 9GH ) )가 ... 생략) ' – cookie