如何从数组创建多个数组?

时间:2011-01-26 17:17:19

标签: php

我有一个同一级别的数组。

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

如何从$original创建像这样的多个数组?

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

感谢

1 个答案:

答案 0 :(得分:1)

这将$ original数组拆分为以'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;