1

My code is as follows:

<?php
$hour_list = array( '' => '',  '0' => 0,  '1' => 1,  '2' => 2,  '3' => 3,  '4' => 4,  '5' => 5,  '6' => 6,  '7' => 7,  '8' => 8,  '9' => 9,  '10' => 10,  '11' => 11,  '12' => 12,  '13' => 13,  '14' => 14,  '15' => 15,  '16' => 16,  '17' => 17,  '18' => 18,  '19' => 19,  '20' => 20,  '21' => 21,  '22' => 22,  '23' => 23,  '24' => 24 ) ;
?>

<script type="text/javascript">
function create_hour_dropdown(){
  var hour_list = <?php echo json_encode($hour_list);?>;
  var hr_list;
  alert(JSON.stringify(hour_list));
  /* Result is the following json string
  {"0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","10":"10","11":"11","12":"12","13":"13","14":"14","15":"15","16":"16","17":"17","18":"18","19":"19","20":"20","21":"21","22":"22","23":"23","24":"24","":""}*/
    for (var key in hour_list) {
              if (hour_list.hasOwnProperty(key)) {
                //alert(key + " -> " + hour_list[key]);
                hr_list += '<option value="'+key+'">'+hour_list[key]+'</option>';
              }
        }
}
    var table = document.getElementById("editview");
    var rowCount = table.rows.length;
    var newRow = table.insertRow(rowCount);
    newRow.id = "vendor_1";
    var newTD9 = document.createElement("td");
    newTD9.align = "left";
    newTD9.setAttribute("class", "ven_hr_min");
    newTD9.innerHTML = '<select id="hmhour_ven_1" name="hmhour_ven_1" size="1" >'+hr_list+'</select>';
    newTD9.setAttribute("colspan", "2");
    newRow.appendChild (newTD9);
</script>

I am confused why the first array value became last in the jSON string. I want the JS array to remain the same as PHP array. Please help! Thanks!

Edit: I have edited the answer. The answer to the question which is being said to be a possible duplicate says

There is nothing in the docs that explicitly confirms that the order of array items is preserved. However, the docs state that for non-array properties, order is not guaranteed:

But here I am using json_encode to convert to php array as per this question.

Edit: Edited again to show how I am using it.

surefoot
  • 51
  • 4
  • Can you do a `var_export` instead of a `print_r`? Also, please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and update your question accordingly. Your problem can't be reproduced easily as it currently stands. – Ethan Oct 04 '17 at 17:40
  • You're judging the result of `alert(JSON.stringify(hour_list));` for which the duplicate fits. The actual result of the php `json_encode` call shows your expected order (check the rendered source). https://3v4l.org/Rp9RZ – Yoshi Oct 09 '17 at 10:49
  • @Yoshi JSON.stringify is just to show what the resulting JS array is. My question is not related to JSON.stringify result. It is related to the JS array that I am getting. If I iterate through the hour_list array I get the order of keys as shown by JSON.stringify. – surefoot Oct 10 '17 at 07:22
  • @surefoot That's the hole point. In the javascript part of your code, you don't have an array, you have an object (see the curly braces in the rendered result from the php call). And for objects iteration order is not guaranteed. You get an object, because the php array has [non-numerical/non-sequential keys](https://secure.php.net/manual/en/function.json-encode.php#example-4379). – Yoshi Oct 10 '17 at 07:38
  • @Yoshi but I used json_encode to convert it to an array as per the answer to [link](https://stackoverflow.com/questions/5618925/convert-php-array-to-javascript/5619038#5619038) so it should be an array. – surefoot Oct 10 '17 at 07:48
  • @surefoot Sure you use `json_encode` correctly, but you'll need to realize that php-arrays are not the same as javascript-arrays. And in your case, the php-array will be *json-encoded* as something that, in javascript, is interpreted as an **object**. [Please re-read the examples I linked above](https://secure.php.net/manual/en/function.json-encode.php#example-4379). – Yoshi Oct 10 '17 at 07:53

0 Answers0