The main content of this article is about PHP index arrays and associative arrays. It has certain reference value. Now I share it with everyone. Friends in need can refer to it.
I encountered a bug recently and recorded it. The printing results of
//定义一个数组 $arr = Array('a','b','c','d'); //转为json字符串 $str1 = json_encode($arr); //去掉一项 unset($arr[1]); //转json $str2 = json_encode($arr); var_dump($str1); echo '<br/>'; var_dump($str2);
string(17) "["a","b","c","d"]"
string(25) "{"0":"a","2":"c","3":"d"}"
show that one is an array and the other is an object.
Obviously the first is an index array and the second is an associative array. The index array is converted to json to an array, and the associative array is converted to json to an object.
Directly using unset() to delete array elements will convert the index array into an associative array
How to delete elements and keep the index:
unset($arr[1]); array_values($arr);
array_splice($array, 1, 1);
Related recommendations:
PHP index array is processed into an associative array on demand?
#
The above is the detailed content of php index array and associative array. For more information, please follow other related articles on the PHP Chinese website!