我在php中遇到一个小问题,我知道解决方案很简单,但是我似乎无法解决它。
基本上,我有一个数组,需要用到它的一小部分。
<?php
$main = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$start = 5;
$sliced = array_slice($main, $start);
$offset=3;
$startx = $start;
foreach($sliced as $s)
{
$start_pt = $startx-$offset;
$end_pt = ($startx) - count($main);
$sli = array_slice($main,$start_pt,$end_pt+1);
print_r($sli);
echo "<br>##############################<br>";
$startx++;
}
除了最后一个返回空白的数组之外,上面的方法还行得通,因为长度现在为-1或-0。将其作为长度而不是偏移量。
有什么更简单/更好的方法来做到这一点?
结果:
Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 )
##############################
Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 )
##############################
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )
##############################
Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 )
##############################
Array ( [0] => 7 [1] => 8 [2] => 9 [3] => 10 )
##############################
Array ( [0] => 8 [1] => 9 [2] => 10 [3] => 11 )
##############################
Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 )
##############################
Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 )
##############################
Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )
##############################
Array ( )
##############################
答案 0 :(得分:1)
我认为您的目的是从数组中创建相同长度的重叠范围,因此您可以尝试以下操作:
<?php
$main = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
function overlapped_chunks($array,$start,$length,$overlap=False){
if(!is_array($array)||!is_int($start)||!is_int($length))
return;
$go=false;
$range=[];
for($i=0;false!==key($array)&&NULL!==key($array);$i++){
$temp=[];
list(,$value)=each($array);
if($i===$start){
$go=true;
}
if($go){
$range[]=$value;
}
if(count($range)===$length){
$temp=$range;
$range=array_slice($range,$length-(is_int($overlap)&&$overlap<$length&&$overlap>=0?$overlap:$length-($length-1)));
yield $temp;
}
}
if($value!==end($temp)) yield $range;
}
foreach(overlapped_chunks($main,2,4,3) as $k=>$v){
print_r($v);
echo "<br>##############################<br>";
}
?>
这将打印
Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 )
##############################
Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 )
##############################
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )
##############################
Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 )
##############################
Array ( [0] => 7 [1] => 8 [2] => 9 [3] => 10 )
##############################
Array ( [0] => 8 [1] => 9 [2] => 10 [3] => 11 )
##############################
Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 )
##############################
Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 )
##############################
Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )
##############################
Array ( [0] => 12 [1] => 13 [2] => 14 [3] => 15 )
##############################
它既可以用于有序数组,也可以用于关联数组
答案 1 :(得分:0)
您想使用array_slice()和array_chunk()
轻松地完成操作首先,您需要对数组进行切片以获取偏移量,然后将其拆分为大块
<?php
$arr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$offset = 3;
$start = 5;
$splitedArr = array_slice($arr, $start);
for ($i = 0, $size = floor(count($splitedArr) / $offset); $i < $size; $i += $offset) {
$sli = array_chunk($splitedArr, $offset);
print_r($sli);
}
因此,它的作用是遍历为第一个偏移量切片的数组,然后计算应迭代数组大小/块大小的次数,并在每次迭代时增加块的大小。它实际上在循环内部制作了块并将其打印出来。
输出:
Array
(
[0] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[1] => Array
(
[0] => 9
[1] => 10
[2] => 11
)
[2] => Array
(
[0] => 12
[1] => 13
[2] => 14
)
[3] => Array
(
[0] => 15
[1] => 17
)
)