1

I'm having a small issue in php and i know the solution is straightforward but i can't seem to get around to it.

basically i have an array and i need small slices of it until the end.

<?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++;
}

The above kinda works except for the last array which return blank since the length is now -1 or -0. It's taking it as length instead of offset.

Any easier/better way to do the above?

Result :

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 ( )
##############################
5
  • Why not just add if(empty($s)) continue; to the top of your loop? Will skip any empty ones Commented Jul 31, 2018 at 19:47
  • @GrumpyCrouton - i need the last array which ends in 15 Commented Jul 31, 2018 at 19:52
  • Oh, so the final one would just be Array ( [0] => 15 ) ? Commented Jul 31, 2018 at 19:57
  • Why can't you just use 4 as the length? Commented Jul 31, 2018 at 19:58
  • What I mean is $sli = array_slice($main,$start_pt,4); Commented Jul 31, 2018 at 19:59

2 Answers 2

1

I think your purpose is to make overlapping ranges of same length from your array so you can try this:

<?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>";


}

?>

and this will print

    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 ) 
##############################

It works with both ordered and associative arrays

Sign up to request clarification or add additional context in comments.

Comments

0

What you want to do you can easily do with array_slice() and array_chunk()

first you need to slice array for offset and then split into chunks

<?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); 
}

So what it does it loops through array which was sliced for your first offset then it counts how many times it should iterate size of an array / size of a chunk and on each iteration adds the size of chunk. Inside the loop it actually makes chunk and prints it.

Output:

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.