임이 데이터로 작업 :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] => 배열 ( [주소] => 배열 ( [하나] => 1 A 거리 [두] =>를 도시 [우편 번호] => WC2 9GH ) )가 ... 생략) ' – cookie