Home  >  Article  >  Backend Development  >  What is associative array in php

What is associative array in php

青灯夜游
青灯夜游Original
2022-09-30 16:13:054622browse

In PHP, an associative array is an array whose key name is a string or a mixture of strings and numbers; if a key name in an array is not a number, then the array is an associative array. The key name of an associative array can be any integer or string; if the key name is a string, a delimiting modifier must be added to the key name: single quotation mark "''" or double quotation mark """".

What is associative array in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

Each entity in the array contains two items , respectively key and value. The corresponding array elements can be obtained by key value. These keys can be numeric keys or association keys. If a variable is a container that stores a single value, then an array is a container that stores multiple values.

Array can store different types of data and is a composite data type. The data structure is as follows:

What is associative array in php

In a PHP array, no matter what type of key name there will be a value corresponding to it, that is, a key/value pair, according to Depending on the data type of the array key name, we can divide PHP arrays into two types:

  • Arrays with numbers as key names, that is, indexed arrays (Indexed Array);

  • An array whose key name is a string or a mixture of strings and numbers, that is, an Associative Array.

PHP associative array

The subscript (key name) of the associative array is a mixture of numerical values ​​and strings Composition, if a key name in an array is not a number, then the array is an associative array.

<?php
header("Content-type:text/html;charset=utf-8");
echo "索引数组:";
$arr1 = array(&#39;0&#39; => &#39;苹果&#39;,&#39;1&#39; => &#39;香蕉&#39;,&#39;2&#39; => &#39;橘子&#39;,&#39;3&#39; => &#39;李子&#39;,&#39;4&#39; => &#39;草莓&#39;);
var_dump($arr1);

echo "关联数组:";
$arr2 = 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;);
var_dump($arr2);
$arr3 = array(&#39;苹果&#39;,&#39;香蕉&#39;,&#39;橘子&#39;,&#39;Plum&#39; => &#39;李子&#39;,&#39;草莓&#39;);
var_dump($arr3);
?>

What is associative array in php

The key name of an associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quotes '' or double quotes "". For indexed arrays, in order to avoid confusion, it is best to add delimiters.

Note: The key name cannot be NULL.

Extended knowledge: Index arrays and associative arrays can be converted to each other

1. Convert associative arrays to index arrays

The array_values() function is to return the values ​​of all elements in the array

array_values(array)

It is very simple to use. With only one required parameter, you can return an array containing all the values ​​​​in the given array. , but key names are not preserved. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.

Simply put, you can use this function to reset the array key name and convert the key name with confusing string or numerical value into a numeric key name starting from 0 and increasing by 1.

array_values() function is particularly suitable for arrays with confusing element subscripts (numeric keys can be reset), or for converting associative arrays into indexed arrays.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90,2,3,4);
echo "原数组:";
var_dump($arr);
$res=array_values($arr);
echo "转为索引数组后:";
var_dump($res);
?>

What is associative array in php

2. Convert index array to associative array

array_combine() function creates an array by merging two arrays A new array, in which the elements in the $keys array serve as the keys of the new array, and the elements in the $values array serve as the keys of the new array.

array_combine($keys,$values)

But it should be noted that when using the array_combine() function to create an array, the number of elements in the $keys array and the $values array must be consistent. Only in this way can the key name and key value correspond one-to-one, otherwise an error will be reported and FALSE will be returned.

And the $keys array cannot be a multi-dimensional array, otherwise an error will be reported; But the $values array can be a multi-dimensional array.

<?php
header("Content-type:text/html;charset=utf-8");
$keys=array("a","b","c","d");
$values=array("red","green","blue","yellow");
var_dump($keys);
var_dump($values);
echo "使用array_combine()合并数组后:";
var_dump(array_combine($keys,$values));
?>

What is associative array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is associative array 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