View Full Version : Get everything after some array value

03-05-2009, 10:08 AM
Hi guys,
This is cool question,

I have array and a value that is in that array. I need to get all that come after that value i have in array. Well it's easier to show in code so i'll show it

$values = array ("apple", "bannana", "lemon", "mango", "pineapple");
$valuse = "lemon";

//i would need some kind of function to get this result

$result_array = array("mango", "pineapple");

in the mean time i'm searching php.net/array to find some thing that can maybe help me with this prob..

ty in advance

03-05-2009, 10:16 AM
$array= array ("apple", "bannana", "lemon", "mango", "pineapple");

print_r(after_value($array, 'lemon'));

function after_value($array, $value)
$position = array_search($value, $array);
return array_slice($array, $position + 1);

This is what you need:

03-05-2009, 10:23 AM
Thanks, it certainly looks neater then my try :)

$values = array("apple", "banana", "lemon", "mango", "pineapple");
$value = "lemon";
$key = array_search($value, $values);

for ($i=0; $i<(count($values)-$key); $i++) {
$result_array[$i] = $values[$key+$i];


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum