I have an array that is in a certain order and I want to just cutoff a portion of the array starting from the first index to the index of a given key.

IE... If i had this array

$array = array("0" => 'blue', "1" => 'red', "2" => 'green', "3" => 'red', "4"=>"purple");

I want to cut off the first part of the array before the key "2" (as a string) is seen. So the end array would be something like...

"2" => 'green'
"3" => 'red'

Thanks, Ian

Accepted Answer

For your case you can use

print_r(array_slice($array, 2, count($array),true));

EDIT: For edited question

$cloneArray = $array;
foreach($array as $key => $value){
  if($key == $givenInex)


Then use $cloneArray

Written by Gaurav
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki