Home > Backend Development > PHP Problem > What are the two types of arrays in php?

What are the two types of arrays in php?

青灯夜游
Release: 2023-03-16 20:48:01
Original
3564 people have browsed it

Two types of php arrays: 1. Index array. The subscript (key name) consists of numbers. It automatically increases from 0 by default. Each number corresponds to the position of an array element in the array. 2. Associative array, the subscript (key name) consists of 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.

What are the two types of arrays in php?

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

In the PHP array, there will be a key name The value corresponds to it, which is a key/value pair.

What are the two types of arrays in php?

According to the different data types of array key names, we can divide PHP arrays into two types:

  • An array with numbers as keys is called an Indexed Array;

  • An array with strings or a mixture of strings and numbers as keys is called an Associative Array. ).

1. Index array

The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to The position of an array element in the array does not need to be specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array(1,2,3,4,5,6,7,8,9,10);
var_dump($array);//打印数组
?>
Copy after login

What are the two types of arrays in php?

2. Associative array

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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52);
var_dump($array);//打印数组
?>
Copy after login

What are the two types of arrays 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 quote '' or double quote "". For indexed arrays, in order to avoid confusion, it is best to add delimiters.

Extended knowledge: mutual conversion between index array and associative array

Associative array to index array

In php, you can use the array_values() function to convert an associative array into an index array.

array_values($array) The function is to return the values ​​of all elements in the array. It is very simple to use. With only one required parameter, you can return a value containing all the elements in the given array. Array of values, but no key names. 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, or for converting associative arrays into indexed arrays.

<?php
$arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr);
var_dump(array_values($arr));
?>
Copy after login

What are the two types of arrays in php?

Convert index array to associative array

In php, you can use the array_combine() function to convert the index array into an associative array array.

array_combine($keys,$values)The function creates a new array by merging two arrays, where the elements in the $keys array serve as the keys of the new array, $ The elements of the values ​​array serve as the key values ​​of the new array.

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, so that the key names and key values ​​can 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));
?>
Copy after login

What are the two types of arrays in php?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the two types of arrays in php?. 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