2011-01-26 49 views
0

我有一個在同一級別的數組。如何從數組創建多個數組?

$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

你`$ move_level`例如沒有意義。你想從`$ original`中將相同的4個元素複製到新數組中的多個單獨的數組中? – 2011-01-26 17:23:05

+2

創建新陣列的規則是什麼?你是簡單地放下任何一個帶有「03」的元素,然後把剩餘的元素分成四塊? – 2011-01-26 17:23:31

回答

1

此拆分$原數組子排列在每個單元以 '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;