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' "4"=>'purple'
Thanks, Ian开发者_运维技巧
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)
break;
unset($cloneArray[$key]);
}
Then use $cloneArray
$newarray = array_slice($array,2,-1,true);
Yes u have to use array_slice() function in php for ur solution..............
example code is below,
`
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
`
the above code give u the following output,
Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d )
精彩评论