php index array and associative array

不言
Release: 2023-03-24 17:56:02
Original
3576 people have browsed it

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 &#39;<br/>&#39;;
var_dump($str2);
Copy after login
string(17) "["a","b","c","d"]"
Copy after login
string(25) "{"0":"a","2":"c","3":"d"}"
Copy after login

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);
Copy after login
array_splice($array, 1, 1);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template