WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text box in the admin area for entering the names of categories. Right now, this is what is in it

events homepage

Just like that above. Two categories separated by a space. So now I am trying to get the categories from the database and that is done this way. (I am using SMOF framework)

 echo $data['exclude_categories'];

If I do the above, it will echo out

 events homepage

So then I am trying to get a - sign and a comma placed between each category like so

 //looping through my key to get the values
 $string = explode(" ", $data['exclude_categories'] );
 foreach($string as $cat) {

 //concatenating my values into one string
 $string .= '-' .$cat.",";
 echo $data['exclude_categories']; //for testing purposes
 //trimming the trailing comma from the string
 $string = substr($string, 0, -1); // Delete the last character

My problem is that I get this "Array-events,-homepage" and I don't know why the Array is there

share|improve this question

closed as off topic by Brian Fegter, Chris_O, kaiser, Michael, Wyck Sep 4 '12 at 14:17

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Close voted as off-topic – Brian Fegter Sep 4 '12 at 2:26
up vote 0 down vote accepted

It's because you've created an array with explode and assigned it to $string, then you're appending strings onto it when you use the .= concatenation operator within your foreach loop.

here's a simpler method:

$string = '-' . implode( ',-', explode( ' ', $data['exclude_categories'] ) );
share|improve this answer
Thank you Milo, worked great – Jamie Sep 4 '12 at 2:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.