Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
1  
Close voted as off-topic – Brian Fegter Sep 4 '12 at 2:26

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

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. 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 closed questions here.

1 Answer

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.