Search code examples
phparrayssortingarraylistarray-view

Return 5 items each time sequentially from array


I want to return first 5 items from array.

Once first 5 items are returned, then if I reexecute my function then it can return second 5 items from same array and so on.

How can I do this?

$myArray = [28,32,33,34,37,49,50,52,57,61,62,63,65,66,67,68,70,71,73,74,75,80,81,89];

I need output as below

Outputs:-
    first time $ans = [28,32,33,34,37];
    second time $ans = [49,50,52,57,61];
    third time $ans = [62,63,65,66,67];
    fourth time $ans = [68,70,71,73,74];
    fifth time $ans = [75,80,81,89];

Solution

  • I think a class would benefit this situation:

    <?php
    
    class MyClass {
        
        private $pos = 0;
        
        private $data = [];
        
        function __construct($data) {
            $this->data = $data;
        }
        
        public function next($length = 5) {
            $offset = $this->pos;
            
            $this->pos = $offset + $length;
            
            return array_slice($this->data, $offset, $length);
        }
        
    }
    

    Example usage:

    $class = new MyClass([28,32,33,34,37,49,50,52,57,61,62,63,65,66,67,68,70,71,73,74,75,80,81,89]);
    
    print_r($class->next());
    print_r($class->next());
    print_r($class->next());
    print_r($class->next(10));
    

    Outputs:

    Array
    (
        [0] => 28
        [1] => 32
        [2] => 33
        [3] => 34
        [4] => 37
    )
    Array
    (
        [0] => 49
        [1] => 50
        [2] => 52
        [3] => 57
        [4] => 61
    )
    Array
    (
        [0] => 62
        [1] => 63
        [2] => 65
        [3] => 66
        [4] => 67
    )
    Array
    (
        [0] => 68
        [1] => 70
        [2] => 71
        [3] => 73
        [4] => 74
        [5] => 75
        [6] => 80
        [7] => 81
        [8] => 89
    )
    

    UPDATE

    The updated code below works between executions of the script by serializing the object and persisting it to local storage. I also changed the way the data is retrieved by the script, in the example below it is read from a file called 'data', which makes the script more dynamic, in that you can pass different data sets to the script if needed.

    Data (stored in a file called 'data'):

    28,32,33,34,37,49,50,52,57,61,62,63,65,66,67,68,70,71,73,74,75,80,81,89
    

    Script:

    class MyClass {
    
        private $pos = 0;
    
        private $data = [];
    
        function __construct($data) {
            $this->data = $data;
        }
    
        public function next($length = 5) {
            $offset = $this->pos;
    
            $this->pos = $offset + $length;
    
            return array_slice($this->data, $offset, $length);
        }
    
    }
    
    if(file_exists('class.ser')) {
        $class = unserialize(file_get_contents('class.ser'));
    } else {
        $data = explode(',', file_get_contents('data'));
    
        $class = new MyClass($data);
    }
    
    print_r($class->next());
    
    file_put_contents('class.ser', serialize($class));
    

    The process for the script is as follows:

    1. The script checks for a file called 'class.ser', if it exists then it deserializes the object contained within the file
    2. If the object was deserialized then simply execute the next() method
    3. If the object was not deserialized (as the file does not exist) then instantiate a new object by passing in the data from the 'data' file and execute the next() method
    4. Finally, the object, with the now updated properties, gets serialized and stored back to local storage ready for the next execution

    If you want to re-run the script from the beginning of the data set then you need to remove the 'class.ser' file that is created.