我有一个从 CSV 文件收集的 PHP 数组
然后我删除了标头,现在剩下这个数组 并希望
split/
为每次出现的 TRANS 打破一个数组
所以对于每个 [0] => TRANS
我们都有一个新的数组。
我尝试过使用数组块
foreach (array_chunk($data, 9, true) as $row) {
$data2[] = $row;
}
但TRANS线并不总是第9项
任何帮助将不胜感激
谢谢
蚂蚁
这是我的数组
Array
(
[0] => Array
(
[0] => TRANS
[1] => 138
[2] =>
[3] => 10008710
[4] => APPNT
[5] => COS
[6] =>
[7] =>
[8] =>
[9] =>
[10] => D
[11] =>
[12] =>
[13] =>
[14] => 20180516
)
[1] => Array
(
[0] => MTPNT
[1] =>
[2] => 2477540807
[3] => F
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => 20170925
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[2] => Array
(
[0] => ADDRS
[1] => MTRPT
[2] =>
[3] =>
[4] => 1
[5] =>
[6] => DATA
[7] =>
[8] =>
[9] => DATA
[10] => DATA
[11] => DATA
[12] =>
[13] =>
[14] =>
)
[3] => Array
(
[0] => ASSET
[1] =>
[2] => APPNT
[3] => METER
[4] =>
[5] =>
[6] => E6VG470
[7] => LPG
[8] => 2017
[9] => E6S01910141760
[10] => 0
[11] =>
[12] => LI
[13] =>
[14] =>
)
[4] => Array
(
[0] => METER
[1] =>
[2] => U
[3] => S1
[4] =>
[5] =>
[6] =>
[7] =>
[8] => S
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[5] => Array
(
[0] => REGST
[1] =>
[2] => METER
[3] => 5
[4] => SCMH
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[6] => Array
(
[0] => MKPRT
[1] => MAM
[2] => MPS
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[7] => Array
(
[0] => NAME
[1] => CONT
[2] => Mrs
[3] => M
[4] => CARTER
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[8] => Array
(
[0] => CONTM
[1] => TEL
[2] => 345345345
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[9] => Array
(
[0] => TRANS
[1] => 139
[2] =>
[3] => 10008710
[4] => APPNT
[5] => COS
[6] =>
[7] =>
[8] =>
[9] =>
[10] => D
[11] =>
[12] =>
[13] =>
[14] => 20180516
)
上面的数组以 TRANS 开头的相同模式继续
这是目前使用的代码
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
$data[] = $row;
}
fclose($handle);
}
return $data;
}
$data = csv_to_array('test.csv');
echo'EXTRACTED HEADER';
$header = $data['0'];
echo '<pre>';
print_r($header);
echo '</pre>';
echo'DATA';
array_shift($data);
array_pop($data);
echo '<pre>';
print_r($data);
echo '</pre>';
输出会像这样...
Array
(
[0] => Array
(
[0] => Array
(
[0] => TRANS
[1] => 138
[2] =>
[3] => 10008710
[4] => APPNT
[5] => COS
[6] =>
[7] =>
[8] =>
[9] =>
[10] => D
[11] =>
[12] =>
[13] =>
[14] => 20180516
)
[1] => Array
(
[0] => MTPNT
[1] =>
[2] => 2477540807
[3] => F
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => 20170925
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[2] => Array
(
[0] => ADDRS
[1] => MTRPT
[2] =>
[3] =>
[4] => 1
[5] =>
[6] => DATA
[7] =>
[8] =>
[9] => DATA
[10] => DATA
[11] => DATA
[12] =>
[13] =>
[14] =>
)
[3] => Array
(
[0] => ASSET
[1] =>
[2] => APPNT
[3] => METER
[4] =>
[5] =>
[6] => E6VG470
[7] => LPG
[8] => 2017
[9] => E6S01910141760
[10] => 0
[11] =>
[12] => LI
[13] =>
[14] =>
)
[4] => Array
(
[0] => METER
[1] =>
[2] => U
[3] => S1
[4] =>
[5] =>
[6] =>
[7] =>
[8] => S
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[5] => Array
(
[0] => REGST
[1] =>
[2] => METER
[3] => 5
[4] => SCMH
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[6] => Array
(
[0] => MKPRT
[1] => MAM
[2] => MPS
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[7] => Array
(
[0] => NAME
[1] => CONT
[2] => Mrs
[3] => M
[4] => CARTER
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[8] => Array
(
[0] => CONTM
[1] => TEL
[2] => 345345345
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
)
[1] => Array
(
[0] => Array
(
[0] => TRANS
[1] => 138
[2] =>
[3] => 10008710
[4] => APPNT
[5] => COS
[6] =>
[7] =>
[8] =>
[9] =>
[10] => D
[11] =>
[12] =>
[13] =>
[14] => 20180516
)
[1] => Array
(
[0] => MTPNT
[1] =>
[2] => 2477540807
[3] => F
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => 20170925
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[2] => Array
(
[0] => ADDRS
[1] => MTRPT
[2] =>
[3] =>
[4] => 1
[5] =>
[6] => DATA
[7] =>
[8] =>
[9] => DATA
[10] => DATA
[11] => DATA
[12] =>
[13] =>
[14] =>
)
[3] => Array
(
[0] => ASSET
[1] =>
[2] => APPNT
[3] => METER
[4] =>
[5] =>
[6] => E6VG470
[7] => LPG
[8] => 2017
[9] => E6S01910141760
[10] => 0
[11] =>
[12] => LI
[13] =>
[14] =>
)
[4] => Array
(
[0] => METER
[1] =>
[2] => U
[3] => S1
[4] =>
[5] =>
[6] =>
[7] =>
[8] => S
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[5] => Array
(
[0] => REGST
[1] =>
[2] => METER
[3] => 5
[4] => SCMH
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[6] => Array
(
[0] => MKPRT
[1] => MAM
[2] => MPS
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[7] => Array
(
[0] => NAME
[1] => CONT
[2] => Mrs
[3] => M
[4] => CARTER
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[8] => Array
(
[0] => CONTM
[1] => TEL
[2] => 345345345
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
)
[2] => Array
(
[0] => Array
(
[0] => TRANS
[1] => 138
[2] =>
[3] => 10008710
[4] => APPNT
[5] => COS
[6] =>
[7] =>
[8] =>
[9] =>
[10] => D
[11] =>
[12] =>
[13] =>
[14] => 20180516
)
[1] => Array
(
[0] => MTPNT
[1] =>
[2] => 2477540807
[3] => F
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => 20170925
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[2] => Array
(
[0] => ADDRS
[1] => MTRPT
[2] =>
[3] =>
[4] => 1
[5] =>
[6] => DATA
[7] =>
[8] =>
[9] => DATA
[10] => DATA
[11] => DATA
[12] =>
[13] =>
[14] =>
)
[3] => Array
(
[0] => ASSET
[1] =>
[2] => APPNT
[3] => METER
[4] =>
[5] =>
[6] => E6VG470
[7] => LPG
[8] => 2017
[9] => E6S01910141760
[10] => 0
[11] =>
[12] => LI
[13] =>
[14] =>
)
[4] => Array
(
[0] => METER
[1] =>
[2] => U
[3] => S1
[4] =>
[5] =>
[6] =>
[7] =>
[8] => S
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[5] => Array
(
[0] => REGST
[1] =>
[2] => METER
[3] => 5
[4] => SCMH
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[6] => Array
(
[0] => MKPRT
[1] => MAM
[2] => MPS
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[7] => Array
(
[0] => NAME
[1] => CONT
[2] => Mrs
[3] => M
[4] => CARTER
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[8] => Array
(
[0] => CONTM
[1] => TEL
[2] => 345345345
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
)
)
非常简单:
$result = array();
foreach ($data as $row) {
if ($row[0] === "TRANS") {
$result[] = array();
}
$result[count($result) - 1][] = $row;
}
请尝试此代码
$data = csv_to_array('test.csv');
$transArr = array();
$restArr = array();
foreach($data as $singleArr){
if(in_array("TRANS",$singleArr)){
$transArr[] = $singleArr;
}else{
$restArr[] = $singleArr;
}
}
$reslArr = array();
foreach($transArr as $singleA){
$temp = array();
$temp[] = $singleA;
$reslArr[] = array_merge($temp,$restArr);
}
echo '<pre>';print_r($reslArr);echo '</pre>';