-1

My String is a comma separated list of id numbers e.g.

81234567,81234569,44489000,123,9585,1023

I want to convert each part to an array, without the comma, so i can run a foreach, to query an API for each reference number, what is the easiest way of achieving this?

1

1 Answer 1

6

You would use explode():

$parts = explode(',', '1234567,81234569,44489000,123,9585,1023');
print_r($parts);

Array
(
    [0] => 1234567
    [1] => 81234569
    [2] => 44489000
    [3] => 123
    [4] => 9585
    [5] => 1023
)
3
  • 1
    And, if you want to convert array to string. You can use us2.php.net/manual/en/function.implode.php Commented Dec 4, 2013 at 15:16
  • Great thanks for That! I now for each of those id's want to perform an action (query API and echo response), how do i use foreach to achieve this, or should i use while? Commented Dec 4, 2013 at 15:23
  • foreach is generally preferred over while but both would work for you.
    – John Conde
    Commented Dec 4, 2013 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.