title: php javascript select with default value
body: <p>I have some city lists in associative arrays in php. I use javascript "on change" to get the value on each items. I want to set an item as default (I think I am considering using geo location in future).</p>
  
  <p>Here my arrays:</p>
  
  <pre><code>// Array lists
  $city_array = array(
      array('id' =&gt; '1', 'name' =&gt; 'Aaaa'),
      array('id' =&gt; '2', 'name' =&gt; 'Bbbb'),
      array('id' =&gt; '3', 'name' =&gt; 'Cccc'),
      array('id' =&gt; '4', 'name' =&gt; 'Dddd'),
      array('id' =&gt; '5', 'name' =&gt; 'Eeee'),
  );
  $json_city = json_encode($city_array);
  </code></pre>
  
  <p>And my php function:</p>
  
  <pre><code>// Decode
  $cities = json_decode($json_city);
  function display_cities() {
      global $cities;
      $output = '&lt;select id="citylists" onChange="onSelectCity()"&gt;';
      foreach( $cities as $city ) {
          $output .= '&lt;option class="itemcity" value="'.$city-&gt;id.'"&gt;'.$city-&gt;name.'&lt;/option&gt;';
      }
      $output .= '&lt;/select&gt;';
      $output .= '&lt;script type="text/javascript"&gt;
          function onSelectCity() {
              var cityLists = document.getElementById("citylists");
              console.log(cityLists.value);
          }
          window.onload = function() {
              onSelectCity();
          }
      &lt;/script&gt;';
      echo $output;
  }
  </code></pre>
  
  <p>I want the output is like this (printed in HTML):</p>
  
  <pre><code>&lt;select id="citylists"&gt;
      &lt;option class="itemcity" value="1"&gt;Aaaa&lt;/option&gt;
      &lt;option class="itemcity" value="2"&gt;Bbbb&lt;/option&gt;
      &lt;option class="itemcity" value="3" selected&gt;Cccc&lt;/option&gt;
      &lt;option class="itemcity" value="4"&gt;Dddd&lt;/option&gt;
      &lt;option class="itemcity" value="5"&gt;Eeee&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>As you can see, I set the default value is <code>&lt;option class="itemcity" value="3" selected&gt;Cccc&lt;/option&gt;</code>.</p>
  
  <p>How can I do that inside dynamic php loop?</p>
  
  <p>Thanks</p>