Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

php - How Create Multiple array from a array?

I have an array in the same level.

$original = Array(
        0=>"03ssss",//substr("03ssss",0,2)="03" => parent index

          1=>"04aaaa",
          2=>"05absd",
          3=>"07sdsa",
          4=>"08sdsd",

        5=>"03tttt", //substr("03tttt",0,2)="03" => parent index

          6=>"04xxxx, //substr("03xxxx",0,2)="04" => child index
            7=>"05sdds",
            8=>"07sdfd",
            9=>"08sdff",
          10=>"04xsax", //substr("03xxxx",0,2)="04" => child index
            11=>"05sdfs",
            12=>"07sdfds",
            13=>"08asap",
    )

How Can create from $original to multiple array something like this?

$move_level = Array(
 0=>array(0=>"04aaaa 05absd 07sdsa 08sdsd"),
 1=>array(0=>"04xxxx 05sdds 07sdfd 08sdff", 
          1=>"04xsax 05sdfs 07sdfds 08asap") 
);

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This splits the $original array into sub arrays at each element starting with '03':

$move_level = array();
$ary = array();
foreach($original as $value) {
    if (strpos($value, '03') === 0) {
        $move_level[] = $ary;
        $ary = array();
    } else {
        $ary[] = $value;
    }
}
$move_level[] = $ary;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...