Ognj3n
Ognj3n

Reputation: 759

How to sort every nth element of an array into different subarrays?

I have an array like this:

$arrayToFill=['a','b','c','d','e','f','g','h','k','l','m','n','o','p','r','u','w','x','y','z'];

So there are 20 elements and I want array $batchArray to look like this after iteration:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                    [3] => d
                    [4] => e
                )

            [1] => Array
                (
                    [0] => f
                    [1] => g
                    [2] => h
                    [3] => k
                    [4] => l
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => m
                    [1] => n
                    [2] => o
                    [3] => p
                    [4] => r
                )

            [1] => Array
                (
                    [0] => u
                    [1] => w
                    [2] => x
                    [3] => y
                    [4] => z
                )

        )

)

So on sixth, eleventh etc. element new subarray is made and on second of that subarray, new bigger array is made. Here is how I tried and what I made up till now:

$a=0;
$i=0;
$arrayToFill=['a','b','c','d','e','f','g','h','k','l','m','n','o','p','r','u','w','x','y','z'];
$batchArray=[];
$batchArray[$a]=[];
$batchArray[$a][$i]=[];

if($i==0){
    $currentNumber = ($i+1)*5;
}else{
    $currentNumber = 5;
}

$currentNumberOfBathes=count($batchArray[$a])+1;
$currentNumberBatch = 2;

foreach($arrayToFill as $array){
    $numberOfElements=count($batchArray[$a][$i]);
    $currentNumberOfBathes=count($batchArray[$a])+1;
    if($numberOfElements>$currentNumber && $numberOfElements<$currentNumber+2){
        echo 'desilo se';
        echo $i;
        $i++;
        $batchArray[$a][$i]=[];
    }

    array_push($batchArray[$a][$i],$array);

}

So here is what I get:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                    [3] => d
                    [4] => e
                    [5] => f
                )

            [1] => Array
                (
                    [0] => g
                    [1] => h
                    [2] => k
                    [3] => l
                    [4] => m
                    [5] => n
                )

            [2] => Array
                (
                    [0] => o
                    [1] => p
                    [2] => r
                    [3] => u
                    [4] => w
                    [5] => x
                )

            [3] => Array
                (
                    [0] => y
                    [1] => z
                )

        )

)

First of all, few of letters are lost somewhere. When I try to limit this subarray that contains 5 letters to increment its index on third like this:

foreach($arrayToFill as $array){

    $numberOfElements=count($batchArray[$a][$i]);
    $currentNumberOfBathes=count($batchArray[$a])+1;
    if($numberOfElements>$currentNumber && $numberOfElements<$currentNumber+2){
        $i++;
        $batchArray[$a][$i]=[];
    }
    if($currentNumberOfBathes>$currentNumberBatch && $currentNumberOfBathes<$currentNumberBatch+2){
        $a++;
        $batchArray[$a]=[];
    }
    array_push($batchArray[$a][$i],$array);

}

I get this output with warning:

Warning: array_push() expects parameter 1 to be array, null given
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                    [3] => d
                    [4] => e
                    [5] => f
                )

            [1] => Array
                (
                    [0] => g
                )

        )

    [1] => Array
        (
            [1] => 
        )

)

So, what happen here and why is even bigger mystery. Please help

Upvotes: 1

Views: 229

Answers (0)

Related Questions