skip to Main Content

I have an array that need to be sliced in the below formate.

$arr =[val1,val2,val3,val4,..........];

$result = [
        [ val1, val2, val3 ], /* First 3 elements */
        [ val4, val5, val6 ], /* next 3 elements */
        
        [ val7 ], /* next 1 element */
        [ val8 ], /* next 1 element */
        
        [ val9, val10], /* next 2 elements */
        [ val11, val12 ], /* next 2 elements */ 


        [ val13, val14, val15 ], /* next 3 elements */
        [ val16, val17, val18 ], /* next 3 elements */
        
        [ val19 ], /* next 1 element */
        [ val20 ], /* next 1 element */
        
        [ val21, val22], /* next 2 elements */
        [ val23, val24 ], /* next 2 elements */ and so on...
    
    /*repeat same with next 3,3,1,1,2,2 elements */
        
]

so I need to convert array in above formate because of my html element showing data in 3,3,1,1,2,2 formate.

Thanks

2

Answers


  1. Just use while loop and iterate over the data. You can use the pattern as a repeatable counter.

    Demo: https://3v4l.org/mnfhP

    $pattern = [3, 3, 1, 1, 2, 2];
    $data = range(1, 40);
    
    $patternValue = current($pattern);
    $patternCount = 0;
    
    $result = [];
    $row = [];
    while ($value = current($data)) {
        if ($patternCount === $patternValue) {
            $patternValue = next($pattern);
            if (false === $patternValue) {
                $patternValue = reset($pattern);
            }
            $patternCount = 0;
            $result[] = $row;
            $row = [];
        }
        $row[] = $value;
        $patternCount++;
        next($data);
    }
    
    echo json_encode($result);
    

    [[1,2,3],[4,5,6],[7],[8],[9,10],[11,12],[13,14,15],[16,17,18],[19],[20],[21,22],[23,24],[25,26,27],[28,29,30],[31],[32],[33,34],[35,36],[37,38,39]]

    Login or Signup to reply.
  2. This solution breaks the original data into chunks according to the sum of all of the individual bits. Then for each chunk – it again splits it down.

    Although there are nested loops, it’s operating on chunks of data and not individual elements.

    $pattern = [3, 3, 1, 1, 2, 2];
    $data = range(1, 40);
    
    // How many elements to take each chunk
    $length = array_sum($pattern);
    // Split the start data into segments
    $chunks = array_chunk($data, $length);
    
    $output = [];
    foreach ($chunks as $chunk) {
        // For each chunk
        $offset = 0;
        foreach ($pattern as $nextChunkSize) {
            // If no more data, exit loop
            if ($offset > count($chunk)) {
                break;
            }
            // Extract the part of the array for this particular bit
            $output[] = array_slice($chunk, $offset, $nextChunkSize);
            // Move start on by amount of data extracted
            $offset += $nextChunkSize;
        }
    }
    
    echo json_encode($output);
    

    gives…
    [[1,2,3],[4,5,6],[7],[8],[9,10],[11,12],[13,14,15],[16,17,18],[19],[20],[21,22],[23,24],[25,26,27],[28,29,30],[31],[32],[33,34],[35,36],[37,38,39],[40]]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search