Search code examples
phparrayslistexplodeimplode

How to get a comma separated list of x items starting from y from another comma seperated list


Say I have a list Poland,USA,England,China,Uruguay,Spain,Taiwan,Monaco. I want to have a list starting on the 3rd item, and have 5 items in total. That means I want to have England,China,Uruguay,Spain,Taiwan.

How can I do this on PHP?


Solution

  • You can simply convert string to array like

    $str = "Poland,USA,England,China,Uruguay,Spain,Taiwan,Monaco";
    $array = explode(",",$str);
    

    and then use array_slice to skip first 2 items

    print_r(implode(",",array_slice($array,2)));