在我的操作中,我得到的数组包含如下数据:
$A = Array
(
[0] => 0302 sa
[1] => 0602 sdsa
[2] => 0602 sada
[3] => 0802 sadasdasd
[4] => 0302 sada
[5] => 0602 saa
[6] => 0602 sdad
[7] => 0802 sds
[8] => 0302 sda
[9] => 0602 sdada
[10] => 0602 sada
[11] => 0802 sdada
[12] => 0302 sda
[13] => 0602 sdads
[14] => 0602 saddas
[15] => 0802 sadas
)
输出将是新数组
$B = Array
(//03
[0] => array(
[0] => 0602 sdsa,//06
[1] => 0602 sada //06
)
//03
[1] => array(
[0] => 0602 saa,//06
[1] => 0602 sdad //06
)
//03
[2] => array(
[0] => 0602 sdada,//06
[1] => 0602 sada //06
)
//03
[3] =>array(
[0] => 0602 sdads,//06
[1] => 0602 saddas//06
)
)
尝试
function TODO($A){
$B = array();
foreach ($A as $key=>$line){
if(substr($line,0,2)=="03"){
$B[] = ???
}
}
谁能帮我解决这个问题?
答案 0 :(得分:2)
尝试:
$B = array();
$flag = 0;
foreach ($A as $key=>$line){
if($flag != 0) {
if($flag == 1) {
$temp = array();
$temp[0] = $line;
$flag++;
} else {
$temp[1] = $line;
$B[] = $temp;
$flag = 0;
}
}
if(substr($line,0,2)=="03"){
$flag = 1;
}
}
注意:我假设两个06
将紧跟03
。
答案 1 :(得分:1)
$B = array( ); foreach( $a as $line ) { if(substr($line,0,2)=="03"){ if( isset( $tmpArray )) $B[] = $tmpArray; $tmpArray = array( ); } else { if( !isset( $tmpArray ) continue; $tmpArray[] = $line; } } if( isset( $tmpArray )) $B[] = $tmpArray;
简单的想法: - )