When try to json_encode a php array variable into javascript variable have an issue:
var duration_options = <?=json_encode($duration_options)?>;
var duration_options_items = '';
$.each(duration_options,function(index, value) {
if(init_act_duration == value){
var selected_option = 'selected=selected';
}else{
var selected_option = '';
}
duration_options_items = duration_options_items + '<option value="'+index+'" '+selected_option+'>'+value+'</option>';
});
duration_options_items = '<select class="form-control select2 select_ajax select_ajax_duration" name="edit_activity_duration" style="width:100%">'+duration_options_items+'</select>';
The PHP array is
Array
(
[0.5] => 0.5
[1] => 1
[1.5] => 1.5
[2] => 2
[2.5] => 2.5
[3] => 3
[3.5] => 3.5
[4] => 4
)
After json_encode when I use that json in my javascript the array is like:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[0.5] => 0.5
[1.5] => 1.5
[2.5] => 2.5
[3.5] => 3.5
)
Just can't understand it why and have no idea how to ordering them properly
json_encodejson_encode(array_values($duration_options));, the decimal keys don't make much sense anyway (keys are equal to values).