skip to Main Content

I have an array with following elements:

$array = ["START","a","a","a","START","b","b","b","START","c","c","c"];

I need to get only the elements of the array AFTER every occurence of the key element "START" into their own array:

Example:

$arr[0] = ["a","a","a"];
$arr[1] = ["b","b","b"];
$arr[2] = ["c","c","c"];

I can then break down each individual array and process how I need.

Thanks!

I’ve played with array slice and such, which seemed to be the closest answer but to no avail.

3

Answers


  1. Design: Collect each element using START as a delimiter.

    Implementation:

    $array = ["START", "a", "a", "a", "START", "b", "b", "b", "START", "c", "c", "c"];
    $result = [];
    $tempArray = [];
    
    // Loop through each element in the original array
    foreach ($array as $element) {
        if ($element === "START") {
            // If we encounter "START" and $tempArray is not empty, add it to $result
            if (!empty($tempArray)) {
                $result[] = $tempArray;
                $tempArray = []; // Reset for the next segment
            }
        } else { // The element was not START, so we add this to the tempArray
            // Collect elements in the temp array
            $tempArray[] = $element;
        }
    }
    
    // Don't forget to add the last collected array if it exists
    if (!empty($tempArray)) {
        $result[] = $tempArray;
    }
    
    // Output the result for verification
    print_r($result);
    

    Note that in PHP, the [] syntax is a shorthand way to append an item without needing to specify an index.

    Login or Signup to reply.
  2. An approach to this would be to loop through each element and spliting on each encounter of ‘Start’

    $array = ['START', 'a', 'a', 'a', 'START', 'b', 'b', 'b', 'START', 'c', 'c', 'c'];
    
    $new_array = []; // new array to write to 
    $counter = 0; // default counter for 'start' occurence
    foreach ($array as $arr) {
        //checking occurance / encounter for 'START'
        if ($arr === 'START') {
            $counter++;
        }
        else {
            if(!$new_array) 
            {
                $counter = 0;
            }
            $new_array[$counter][] = $arr; //add to array
        }
    
    }
    print_r($new_array);
    

    which would give you a result of

    (
    
    [0] => Array
        (
            [0] => a
            [1] => a
            [2] => a
        )
    
    [1] => Array
        (
            [0] => b
            [1] => b
            [2] => b
        )
    
    [2] => Array
        (
            [0] => c
            [1] => c
            [2] => c
        )
    )
    
    Login or Signup to reply.
  3. Simple and Performing Solution – O(n)

    $array = ["START","a","a","a","START","b","b","b","START","c","c","c"];
    $res = [];
    $index = 0;
    foreach ($array as $val) {
        if ($val === 'START') {
            $index++;
        } else if ($index > 0) {
            $res[$index - 1][] = $val;
        }
    }
    var_dump($res);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search