Omzy Posted November 28, 2009 Share Posted November 28, 2009 Basically I'm reading data from a MySQL database, and I want to compare this data to an array. The array is called $subcats and has the following sample data: $subcats=array( 'flowers'=>array('decorations', 'Flowers'), 'balloons'=>array('decorations', 'Balloons'), 'banners'=>array('decorations', 'Banners'), 'fruit-displays'=>array('decorations', 'Fruit Displays'), 'ice-sculptures'=>array('decorations', 'Ice Sculptures'), ); The data I wish to compare against is in a field called 'tags', which contains a string of values separated by a comma, for example: flowers, balloons, banners Now what I want to do is print out a SELECT MULTIPLE control which has the relevant options selected if they exist in that field. Here is the code I have so far (simplified): foreach($row as $key => $value) { echo '<select size="5" name="'.$key.'">'; foreach($subcats as $index1 => $value1) { echo '<option value="'.$index1.'"', $index1==$value ? ' selected="selected"' : null ,'>'.$value1[1].'</option>'; } echo '</select>'; } Now obviously the $index1==$value part is incorrect, and I'm a bit unsure what should be used to do the comparison. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/183252-compare-comma-seperated-values-to-array-index/ Share on other sites More sharing options...
Omzy Posted November 28, 2009 Author Share Posted November 28, 2009 Can anyone help please? To make it easier to understand: $csv="flowers, balloons, banners"; Compare $csv to array indexes of $subcats, echo out the index if true Quote Link to comment https://forums.phpfreaks.com/topic/183252-compare-comma-seperated-values-to-array-index/#findComment-967195 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 if($csv == implode(', ',array_keys($subcats))) { // ... } implode array_keys Quote Link to comment https://forums.phpfreaks.com/topic/183252-compare-comma-seperated-values-to-array-index/#findComment-967203 Share on other sites More sharing options...
Omzy Posted November 29, 2009 Author Share Posted November 29, 2009 No, that isn't quite right. Firstly I don't need to use array_keys() because I'm already getting the keys from my FOREACH loop. And secondly shouldn't the implode() function be run on the $csv? Quote Link to comment https://forums.phpfreaks.com/topic/183252-compare-comma-seperated-values-to-array-index/#findComment-967208 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 I thought you wanted to compare the $csv string to the indexes of the array. Try something like this: $arr = explode(', ', $csv); foreach($row as $key => $value) { echo '<select size="5" name="'.$key.'">'; $c = 0; foreach($subcats as $index1 => $value1) { echo '<option value="'.$index1.'"', $index1==$arr[$c] ? ' selected="selected"' : null ,'>'.$value1[1].'</option>'; $c++; } $c = 0; echo '</select>'; } Quote Link to comment https://forums.phpfreaks.com/topic/183252-compare-comma-seperated-values-to-array-index/#findComment-967212 Share on other sites More sharing options...
Omzy Posted November 29, 2009 Author Share Posted November 29, 2009 That didn't work either. I figured it out myself: echo '<option value="'.$index1.'"', in_array($index1, explode(', ', $value)) ? ' selected="selected"' : null ,'>'.$value1[1].'</option>'; Quote Link to comment https://forums.phpfreaks.com/topic/183252-compare-comma-seperated-values-to-array-index/#findComment-967234 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.