When Iunset ()an element from an array in Ajax, I found a phenomenon that the array becomes an object without permission whenarray_values ​​()is not performed.

The following is the source code. Can anyone understand the cause?

Applicable source code

When you click on "ajax.js", an AJAX event called'my_ajax_test'is executed.

Next, in where the AJAX event is written,unset ()the element from the array, but the problem mentioned earlier occurred there, "ajax.js"responcereturns an object instead of an array for some reason.

  • Answer # 1

    Since the key value of the array will no longer be a sequential number starting from 0
    array_values ​​will re-assign key values ​​

    0,1 =>1,2 =>2,3 =>3]);// [0,1,2,3]
    echo json_encode ([0 =>0,2 =>2,3 =>3]);// {"0": 0, "2": 2, "3": 3}

    In json_encode, the array whose key value is not a sequential number starting from 0 is
    Interpreted as an object