0

I have following PHP code

$selected_items = explode(',' , $vars['value']);

foreach($vars['options'] as $option) {
    $selected = ""; 
    if(in_array($option,$selected_items)){
        $selected = " selected = 'selected'";
      }
        echo "<option" . $selected . ">" . $option . "</option>";
}

  $selected_items:
  array
  0 => string 'Html' (length=4)
  1 => string ' Css' (length=4)
  2 => string ' HTML5' (length=6)
  3 => string ' CSS3' (length=5)
  4 => string ' Javascript' (length=11)

$vars['options']:
array
  0 => string 'Html' (length=4)
  1 => string 'Css' (length=3)
  2 => string 'HTML5' (length=5)
  3 => string 'CSS3' (length=4)
  4 => string 'Javascript' (length=10)
  5 => string 'Dhtml' (length=5)
  6 => string 'Actionscript' (length=12)
  7 => string 'Javafx' (length=6)
  8 => string 'Flex' (length=4)
  9 => string 'VisualBasic' (length=11)
  10 => string 'Ajax' (length=4)
  11 => string 'ASP.NET' (length=7)
  12 => string 'Java' (length=4)
  13 => string 'Php' (length=3)
  14 => string 'Perl' (length=4)
  15 => string 'Python' (length=6)
  16 => string 'J2ME' (length=4)
  17 => string 'VB.NET' (length=6)
  18 => string 'C#' (length=2)
  19 => string 'ASP' (length=3)
  20 => string 'JSP' (length=3)
  21 => string 'J2EE' (length=4)
  22 => string 'C++' (length=3)

In $selected_items have an array. So i have to check whether the value in $option is in $selected_items. But its working for first time. But second time the value is in array but the condition get false.

Any idea where is the problem ?

6
  • 2
    Can you post what you get in $selected_items & $vars['options'] ? Commented Jun 18, 2014 at 6:59
  • @Rikesh Please see the question now.. i have edited Commented Jun 18, 2014 at 7:38
  • Post a dump of $vars['options'] Commented Jun 18, 2014 at 8:13
  • @Sal00m Yes i edited the question please check Commented Jun 18, 2014 at 8:16
  • 1
    @RIADev I guess you answered yourself, good Commented Jun 18, 2014 at 8:47

2 Answers 2

1

I thing it should work if that string present in array. If you have problem with above one you can try this:

$selected_items = explode(',' , $vars['value']);

foreach($vars['options'] as $option) {
    $selected = ""; 
    foreach($selected_items as $selecteditems){
    if($option == $selecteditems)){
        $selected = " selected = 'selected'";
      }}
        echo "<option" . $selected . ">" . $option . "</option>";
}
Sign up to request clarification or add additional context in comments.

7 Comments

@RIADev you mean to ask mukrisnaa this not me :-)
sorry @RIADev @Darren changed my code when editing please now try the above code
@mukrishnaa Just formatted it correctly, didn't change a word.
@RIADEV can i know which string you checked now? Because in $slected_items you have same string only for Html ,others are having space before the string start.
please check your $selected_items in that other than Html all are had spaces before string.listen length of string.
|
0

Just remove the space before string in array $selected_items.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.