CyberShot Posted April 7, 2011 Share Posted April 7, 2011 I am working with wordpress. I am using a plugin that gets options stored in an array in the database. This is how I get the options $options = get_option('option_tree'); echo $options['exc_cat']; the exc_cat is a key that stores a set of values. Right now with the code above, it echoes Array. If I do this echo $options['exc_cat'][0]; it returns 5. Which is what I would expect it to do. What I can't figure out is how to get it to store each value which should be a number into 1 variable. What I need to do is loop through each value that is stored in the exc_cat key, return the number seperated by a -1,. So the result should look like this -1, -5 1 and 5 should be the only two numbers stored in the array at this point. Can you help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233021-help-with-array-and-loop/ Share on other sites More sharing options...
AbraCadaver Posted April 7, 2011 Share Posted April 7, 2011 foreach($options['exc_cat'] as $cat) { echo "-{$cat}"; } Quote Link to comment https://forums.phpfreaks.com/topic/233021-help-with-array-and-loop/#findComment-1198448 Share on other sites More sharing options...
CyberShot Posted April 7, 2011 Author Share Posted April 7, 2011 Wow!, that was a whole lot easier than I thought it was going to be. Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/233021-help-with-array-and-loop/#findComment-1198451 Share on other sites More sharing options...
CyberShot Posted April 7, 2011 Author Share Posted April 7, 2011 okay, one more question. How do I use that out of the foreach loop? I need to put {$cat} into a wordpress query Quote Link to comment https://forums.phpfreaks.com/topic/233021-help-with-array-and-loop/#findComment-1198453 Share on other sites More sharing options...
CyberShot Posted April 7, 2011 Author Share Posted April 7, 2011 well, I almost have it working. It removes one category but I think it is failing to remove the next one because of a trailing , It is returning -1,-5, instead of -1, -5 foreach($options['exc_cat'] as $cat) { query_posts($query_string . '&cat=-' . $cat . ','); } This is the only way I could get it to remove the category. One still stays on the page. Quote Link to comment https://forums.phpfreaks.com/topic/233021-help-with-array-and-loop/#findComment-1198480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.