我有一个从CSV文件收集的PHP数组
然后我删除了Header,现在剩下这个数组了
并希望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] =>
)
)
)
答案 0 :(得分:0)
请尝试使用此代码
$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>';
答案 1 :(得分:0)
这很简单:
$result = array();
foreach ($data as $row) {
if ($row[0] === "TRANS") {
$result[] = array();
}
$result[count($result) - 1][] = $row;
}