Getting duplicated results in foreach loop

半腔热情 提交于 2021-02-11 07:54:47

问题


I'm trying to make checkboxes to be checked by default based on the user's information retrieved from mysql. The shipping countries is a field that stores a string like USA|UK|Asia. To see if each checkboxes should be checked, I make an array check_countries to check against the retrieved data like this

        $country_html ="";
        $check_countries = array("Europe","Americas","USA","Australia","UK","Africa","Asia","Global");

        $shipping_countries = explode("|",$shipping_countries); // e.g USA|UK|Asia
        foreach($shipping_countries as $shipping_country)
        {
          foreach($check_countries as $check_country)
          {
              if($check_country == $shipping_country)
              {
                 $country_html .= "<label for='".$check_country."'><input id='".$check_country."' type='checkbox' name='shipcountries[]' value='".$check_country."' checked>".$check_country."</label>";
              } 
              else
              {
                 $country_html .= "<label for='".$check_country."'><input id='".$check_country."' type='checkbox' name='shipcountries[]' value='".$check_country."'>".$check_country."</label>";
              }
           }
         }

However, I'm getting duplicated results. Every checkbox has its own twin bother next to each other:

My expected output should be:

<label for="Europe"><input id="Europe" type="checkbox" name="countries[]" value="Europe">Europe</label>
<label for="Americas"><input id="Americas" type="checkbox" name="countries[]" value="Americas">Americas</label>
<label for="USA"><input id="USA" type="checkbox" name="countries[]" value="USA" checked>USA</label>
<label for="Australia"><input id="Australia" type="checkbox" name="countries[]" value="Australia">Australia</label>
<label for="UK"><input id="UK" type="checkbox" name="countries[]" value="UK" checked>UK</label>
<label for="Africa"><input id="Africa" type="checkbox" name="countries[]" value="Africa">Africa</label>
<label for="Asia"><input id="Asia" type="checkbox" name="countries[]" value="Asia" checked>Asia</label>
<label for="Global"><input id="Global" type="checkbox" name="countries[]" value="Global">Global</label>

But now it returns duplicated results:

○Europe ○Europe ○Americas ○Americas ○USA ○USA ○Australia ○Australia ○UK ○UK ○Africa ○Africa ○Asia ○Asia ○Global ○Global

<label for="Europe"><input id="Europe" type="checkbox" name="countries[]" value="Europe" checked="">Europe</label>
<label for="Europe"><input id="Europe" type="checkbox" name="countries[]" value="Europe">Europe</label>
<label for="Americas"><input id="Americas" type="checkbox" name="countries[]" value="Americas">Americas</label>
<label for="Americas"><input id="Americas" type="checkbox" name="countries[]" value="Americas">Americas</label>
<label for="USA"><input id="USA" type="checkbox" name="countries[]" value="USA">USA</label>
<label for="USA"><input id="USA" type="checkbox" name="countries[]" value="USA">USA</label>
<label for="Australia"><input id="Australia" type="checkbox" name="countries[]" value="Australia">Australia</label>
<label for="Australia"><input id="Australia" type="checkbox" name="countries[]" value="Australia">Australia</label>
<label for="UK"><input id="UK" type="checkbox" name="countries[]" value="UK">UK</label>
<label for="UK"><input id="UK" type="checkbox" name="countries[]" value="UK">UK</label>
<label for="Africa"><input id="Africa" type="checkbox" name="countries[]" value="Africa">Africa</label>
<label for="Africa"><input id="Africa" type="checkbox" name="countries[]" value="Africa" checked="">Africa</label>
<label for="Asia"><input id="Asia" type="checkbox" name="countries[]" value="Asia">Asia</label>
<label for="Asia"><input id="Asia" type="checkbox" name="countries[]" value="Asia">Asia</label>
<label for="Global"><input id="Global" type="checkbox" name="countries[]" value="Global">Global</label>
<label for="Global"><input id="Global" type="checkbox" name="countries[]" value="Global">Global</label>

Can anyone tell me how to fix that?


回答1:


You are doing an extra foreach, just check if the key exists in the array, you can do that using in_array. And to show all, just reverse the arrays order

<?php

$shipping_countries = "USA|UK|Asia";
$check_countries = array("Europe","Americas","USA","Australia","UK","Africa","Asia","Global");

$shipping_countries = explode("|",$shipping_countries); // e.g USA|UK|Asia
foreach($check_countries as $check)
{
  if(in_array($check, $shipping_countries))
  {
     $country_html .= "<label for='".$check."'><input id='".$check."' type='checkbox' name='shipcountries[]' value='".$check."' checked>".$check."</label>";
  } 
  else
  {
     $country_html .= "<label for='".$check."'><input id='".$check."' type='checkbox' name='shipcountries[]' value='".$check."'>".$check."</label>";
  }
 }

echo $country_html;


来源:https://stackoverflow.com/questions/25209631/getting-duplicated-results-in-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!