-1

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.

1

4 Answers 4

0

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.

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

Comments

0

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

1 Comment

0

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

1 Comment

0

Use two build-in functions array_key with second argument as "START" to find keys with that value and arrya_slice to take a part of the input array

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

// Find all positions of START in the array
$keys = array_keys($array, 'START', True);
//.Save the first array start
$prev = array_shift($keys) + 1;
// Add length of the array as the last array end
array_push($keys, count($array));
$res = [];
foreach($keys as $x) {
    // Take part of the array to the result
    $res[] = array_slice($array, $prev, $x-$prev);
    // Next array started 
    $prev = $x+1;
}

print_r($res);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.