0

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

7
  • 1
    After json_encode becomes: ... Is that really a Javascript array? Commented Apr 21, 2016 at 10:17
  • check this:- eval.in/557376 . It clearly states that you are doing something with your array before json_encode Commented Apr 21, 2016 at 10:24
  • Please edit the question and post your code. Don't just assume PHP is broken ;-) Commented Apr 21, 2016 at 10:26
  • @Anant Not necessarily, just paste the encoded json in your browser console (chrome in my case), and you'll see the reordered version. Problem is, the question is probably missing the "when I use that json in my javascript..." portion. Commented Apr 21, 2016 at 10:26
  • 1
    Just use json_encode(array_values($duration_options));, the decimal keys don't make much sense anyway (keys are equal to values). Commented Apr 21, 2016 at 10:32

3 Answers 3

2

You are observing this behaviour because your array does not contain integer and sequential keys. So, json_encode maps it to a JSON object.

Sign up to request clarification or add additional context in comments.

Comments

2

I presume you assume the JSON is wrong but you haven't really inspected it (there're several ways to do so, such as the Net pane in your browser's developer tools).

As soon as you parse it from JavaScript it becomes a JavaScript object. As such, the order of keys is not guaranteed because that's how the ECMAScript spec defines objects.

If you need bullet-proof order, you need to switch to regular arrays, i.e., with consecutive zero-based integer keys.

Comments

-1

And what is the problem with this, you have same keys so you can use them the same? It seems it sort by length key and then natural.

When you loop use keys.

1 Comment

I edited my question to see that I use the json_encode php variable to make select / option dropdown with loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.