Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that your objects of ISO CODES acts like an array and not like an object. When you add an item to an array with numeric index, it will put it in a specific order in that array automatically, that's the way array works in javascript.<br> For example, if you try this code it Chrome's console:</p> <pre><code>var arr = []; arr[4] = "data"; //same for arr['4'] - javascript will convert it to arr[4] arr; </code></pre> <p>It will show you the following result:</p> <pre><code>[undefined × 4, "data"] </code></pre> <p>Which means that the it'll create all 5 indexes (from 0 to 4), when the first 4 will be undefined, and the fifth one (index 4) will be the value you set.<br> Now if you'll add another index which is lower than 4, let's say <code>arr[2] = "Z Data";</code>, now the array will be:</p> <pre><code>[undefined × 2, "Z Data", undefined × 1, "data"] </code></pre> <p>As you see now, the value ordered by it's natural array's way (No matter the content and the insert order).</p> <p>In your case, if you want it to be ordered, there are several options to complete it:<br> <strong>1</strong> - The best option is to create an ordered array that contains an object of your data:</p> <pre><code>var countries = new Array(); {foreach from=$countries item='country'} {if isset($country.states)} countries[{$country.id_country|intval}] = new Array(); {foreach from=$country.states item='state' name='states'} countries[{$country.id_country|intval}].push({state_id: {$state.id_state|intval}, iso: '{$state.name|escape:'htmlall':'UTF-8'}'}); {/foreach} {/if} {/foreach} </code></pre> <p>Which will be compiled into:</p> <pre><code>var countries = new Array(); countries[10] = new Array(); countries[10].push({id: 53, iso:'AG'}); countries[10].push({id: 54, iso:'AL'}); countries[10].push({id: 55, iso:'AN'}); countries[10].push({id: 56, iso:'AO'}); countries[10].push({id: 58, iso:'AP'}); countries[10].push({id: 93, iso:'AQ'}); countries[10].push({id: 57, iso:'AR'}); countries[10].push({id: 59, iso:'AT'}); countries[10].push({id: 60, iso:'AV'}); //etc... </code></pre> <p><strong>2</strong> - Another option is to convert the array into object by prefixing the array's index with string:</p> <pre><code>var countries = new Array(); {foreach from=$countries item='country'} {if isset($country.states)} countries[{$country.id_country|intval}] = {}; //object initialization {foreach from=$country.states item='state' name='states'} countries[{$country.id_country|intval}]['s{$state.id_state|intval}'] = '{$state.name|escape:'htmlall':'UTF-8'}'; {/foreach} {/if} {/foreach} </code></pre> <p>Which will be compiled into:</p> <pre><code>var countries = new Array(); countries[10] = {}; countries[10]['s53'] = 'AG'; countries[10]['s54'] = 'AL'; countries[10]['s55'] = 'AN'; countries[10]['s56'] = 'AO'; countries[10]['s58'] = 'AP'; countries[10]['s93'] = 'AQ'; countries[10]['s57'] = 'AR'; countries[10]['s59'] = 'AT'; countries[10]['s60'] = 'AV'; //ect... </code></pre> <p>Once you use it as an object, it will order the list the order you set it. </p> <p><strong>3</strong> - This option is about converting the array into object too, but this time switching the index with the value:</p> <pre><code>var countries = new Array(); {foreach from=$countries item='country'} {if isset($country.states)} countries[{$country.id_country|intval}] = {}; //object initialization {foreach from=$country.states item='state' name='states'} countries[{$country.id_country|intval}]['{$state.name|escape:'htmlall':'UTF-8'}'] = {$state.id_state|intval}; {/foreach} {/if} {/foreach} </code></pre> <p>Which will be compiled into:</p> <pre><code>var countries = new Array(); countries[10] = {}; countries[10]['AG'] = 53; countries[10]['AL'] = 54; countries[10]['AN'] = 55; countries[10]['AO'] = 56; countries[10]['AP'] = 58; countries[10]['AQ'] = 93; countries[10]['AR'] = 57; countries[10]['AT'] = 59; countries[10]['AV'] = 60; //ect... </code></pre> <p><strong>4</strong> - Of corse the best option is to encode an ordered object (as described in option 1) into a JSON on the server side (PHP), and decode it on the client side (JavaScript), but it takes more time to fix it.</p> <p>Hope it helps, and you understand the behavior of the array in JavaScript.</p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload