2012-11-14 101 views
0

之间我与数据载列的阵列如下PHP分裂阵列在值

[0] =>"npage:new", 
[1] =>"data:data", 
[2] =>"data:data", 
[3] =>"npage:new", 
[4] =>"data:data", 
[5] =>"data:data", 
[6] =>"data:data", 
[7] =>"npage:new", 
[8] =>"data:data", 
[9] =>"data:data", 

进出口试图重新排列阵列,使得在每个“NPAGE:新的”它为它创建的数据的另一索引和下一个“npage:new”。直到最后一组没有“npage:new”但仍然需要索引。 例子:

[0] =>  
    "data:data", 
    "data:data", 
[1] => 
    "data:data", 
    "data:data", 
    "data:data", 
[2] => 
    "data:data", 
    "data:data", 

我有以下,但它返回一个空数组&我想我是在复杂的事情。

$source[] = the array 
    $destination = array(); 
$startval = "new"; 
$found = 0; 
$starindex = 0; 

foreach ($source as $index => $value){ 
if ($value === $startval) { 
$found = $value; 
$startindex = $index; 
} 
    elseif ($value === $found) { 
$destination[] = array_slice($source, $startindex, $index - $startindex + 1); 
$found = 0; 
} 
} 

回答

0

参见:http://codepad.org/QCWTAhnq

<?php 
$source = array("npage:new", "data:data", "data:data", "npage:new", "data:data", "data:data", "data:data", "data:data", "npage:new", "data:data", "npage:new", "data:data", "data:data"); 
$final = array(); 
$newpage = "npage:new"; 

$temp = array(); 

foreach ($source as $index => $value) { 
    if ($value == $newpage) { 
     if(!empty($temp)) { 
      $final[] = $temp; 
      $temp = array(); 
     } 
    } 
    else { 
     $temp[] = $value; 
    } 
} 

if(!empty($temp)) 
    $final[] = $temp; 

print_r($final); 
?> 
+0

感谢!这并获得成功。 – brad

0

这是一个很长的时间,因为我不写PHP的,但尝试这样的事:

$source[] = the array 
$destination = array(); 
$startval = "new"; 
$ix = -1; 

foreach ($source as $index => $value) { 
    if ($value === $startval) { 
     $ix++; 
     $dest[$ix] = array(); 
    } 
    else { 
     array_push($dest[$ix], $index.":".$value; 
    } 
}