开发者

convert existing array to a new one?

In my manipulate I got an array contain data like this:

 $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] =&开发者_开发百科gt; 0602 saddas
           [15] => 0802 sadas
    )

the output will be the new array

 $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
        )
    )

Try

function TODO($A){
  $B = array();
  foreach ($A as $key=>$line){
 if(substr($line,0,2)=="03"){
     $B[] = ???
   }
}

Who can help me to solve this?


Try:

$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;
        }
}

Ideone Link

Note: I'm assuming the two 06's will immediately follow the 03.


$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;

Just simple idea :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜