Home  >  Article  >  Backend Development  >  The difference between php associative array and index array

The difference between php associative array and index array

(*-*)浩
(*-*)浩Original
2019-10-09 11:14:174216browse

In PHP, an array is a collection of a series of data, forming an operable whole. Each data is an element, and the element contains a key name and a key value.

The difference between php associative array and index array

The difference between index array and associative array is distinguished by key name

1. Index array (Recommended learning: PHP video tutorial)

<?php
    $arr = array(&#39;华为&#39;,&#39;三星&#39;,&#39;vivo&#39;,&#39;oppo&#39;);
    print_r($arr);
    echo &#39;<br/><br/><br/>&#39;;
    echo &#39;数组 $arr 中的,键名为2的键值为:&#39;.$arr[2];
?>

The results show:

The difference between php associative array and index array

2. Associative array

<?php
    $arr1 = array(&#39;Apple&#39; => &#39;苹果&#39;,&#39;Banana&#39; => &#39;香蕉&#39;,&#39;Orange&#39; => &#39;橘子&#39;,&#39;Plum&#39; => &#39;李子&#39;,&#39;Strawberry&#39; => &#39;草莓&#39;);
    print_r($arr1);
?>

The result shows:

The difference between php associative array and index array

Arrays with numbers as key names are generally called index arrays and associative arrays The key values ​​are strings and are artificially specified.

The above is the detailed content of The difference between php associative array and index array. 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