By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

implode an array for query condition

P: 8
Hi All,

I'm newish to php & have an issue on a project I'm working on...

I've an array containing zip codes which displays if I echo it like
Expand|Select|Wrap|Line Numbers
  1. echo "$key";
it returns 926299262492693926779267592673926079267492672

I need to implode/explode with commas so it passes like 92629,92624,92693,92677,92675,92673,92607,92674,92 672 so I can use it as a WHERE IN condition.

What's the best way to do this?

Thanks
Karl
Feb 28 '07 #1
Share this Question
Share on Google+
6 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Hi All,

I'm newish to php & have an issue on a project I'm working on...

I've an array containing zip codes which displays if I echo it like
Expand|Select|Wrap|Line Numbers
  1. echo "$key";
it returns 926299262492693926779267592673926079267492672

I need to implode/explode with commas so it passes like 92629,92624,92693,92677,92675,92673,92607,92674,92 672 so I can use it as a WHERE IN condition.

What's the best way to do this?

Thanks
Karl
$key is not an array! The value you show via the "echo $key;" here is a normal string value. So you cannot plode it. Unless you have made a mistake

Ronald :cool:
Feb 28 '07 #2

P: 8
Kinda new/part-time with php, learning every time I sit down with it, please excuse my ignorance.
OK, I guess I'm on the wrong path. I'll detail things a little...
I'm using a class for querying a single table zip code db. I'm adding a table of companies and making a company locator based on range from originating zip code.
I have
Expand|Select|Wrap|Line Numbers
  1. $z = new zipcode_class;
  2. $zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
  3. if ($zips === false) echo 'Error: '.$z->last_error;
  4. else {
  5.  
  6.    foreach ($zips as $key => $value) {
  7.      $query_rsTech = "SELECT * FROM tech_info WHERE tech_zip_code = '$key,'";
  8.      $rsTech = mysql_query($query_rsTech) or die(mysql_error());
  9.      $row_rsTech = mysql_fetch_assoc($rsTech);
  10.    }
  11. }
Should I replace the foreach to $zips as $array then check with an is_array?

Thanks, learning as I go...
Karl
Mar 1 '07 #3

P: 8
Can I make the string ($key) an array to use in a query, maybe?

Karl
Mar 2 '07 #4

100+
P: 268
Count the total number and split the string 5 and the store the valus on single array I hope thsi idea is not bad.
Mar 2 '07 #5

P: 8
I'm gonna try that... After iterating through the first array with
Expand|Select|Wrap|Line Numbers
  1. foreach ($zips as $key => value)
I need to split a string that looks like
926299262492693926779267592673926079267492672
into one that looks like
92629,92624,92693,92677,92675,92673,92607,92674,92 672
so it can be used in a WHERE clause.

I've been reading & trying diffrent things, no success yet.
Any suggestions?
Mar 3 '07 #6

P: 8
Can we bounce this one? I re-started it under a more descriptive title.

Thanks,
Karl
Mar 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.