Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Get everything after some array value

    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

    PHP Code:
    $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

  • #2
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    PHP Code:
    $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

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Thanks, it certainly looks neater then my try

    PHP Code:
    <?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);
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •