...

View Full Version : Get everything after some array value



funnymoney
03-05-2009, 11: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

kokjj87
03-05-2009, 11: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:
http://php.net/array_search
http://php.net/array_slice

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


<?php
$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];
}

print_r($result_array);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum