0

I have a field in my database which holds id's. An example would be: 1,2,3,4,5

I basically need to take that string and make it into 1 array so I can do a query with: WHERE id IN ($string_array).

2 Answers 2

2

PHP: explode - Manual

$string = "1,2,3,4,5";
$array = explode(",", $string);

$query = "SELECT * FROM `table` WHERE id IN (".$string.")";
$query = mysql_query($query);
Sign up to request clarification or add additional context in comments.

5 Comments

That's not quite what I need. That would output it so I would need to do $array[0], $array[1] etc. I need it in one array like array(1,2,3,4,5)
Your array you mentioned in your original question (represented here by $string) is already properly formatted for an SQL IN statement...
But I can't just do IN ($string), it needs to be in an array like array(1,2,3,4,5). I have tried using explode, then looping through and adding to an array but so far haven't been successful. It's pretty frustrating seeing as I have done it before not that long ago.
@RyanStewart I've updated my answer with an SQL example. In this new case, you wouldn't even need to use explode().
Yes, I got it working. I'm using a CMS powered by CodeIgniter. I found just exploding the string then doing a db->where_in($array); worked. Thanks for your help!
1

I think this is what you're looking for:

$array = explode(",",$string)

Comments

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.