Home  >  Article  >  Backend Development  >  The difference between associative arrays and indexed arrays in php

The difference between associative arrays and indexed arrays in php

(*-*)浩
(*-*)浩Original
2019-09-06 13:56:514228browse

The difference between associative arrays and indexed arrays in php

Index array

An array with numbers as key names is generally called an index array. An array whose keys are represented by strings is an associative array that will be introduced below. The keys of the index array are integers, and start from 0 and so on.

Index array initialization example: (Recommended learning: PHP programming from entry to proficiency)

<?php  
//创建一个索引数组,索引数组的键是“0”,值是“苹果”  
$fruit=array("苹果","香蕉");
print_r($fruit);
?>

Running results:

Array
(
[0] => 苹果
[1] => 香蕉
)

Three assignment methods for index arrays:

1.array[0]=&#39;苹果&#39;;
2.$arr=array(&#39;0&#39;=>&#39;苹果&#39;);
3.$arr=array(&#39;苹果&#39;);

Associative array

In fact, the difference between associative arrays and index arrays is only in the key value. The key value of the associative array is a string, and it is an artificial provision, for example:

<?php  
//创建一个关联数组,关联数组的键“orange”,值是“橘子”  
$fruit=array(&#39;orange&#39;=>&#39;橘子&#39;);
echo $fruit[&#39;orange&#39;];
?>

The rest of the initialization, assignment, and foreach usage are basically the same.

The above is the detailed content of The difference between associative arrays and indexed arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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