PHP arrays and data structures
Arrays are used in 30% of places in PHP programming, which shows the importance of PHP arrays.
Array in php
Overview of arrays --- PHP is a weakly typed language, so arrays can store any number of data of any type, and can realize the functions of data structures such as heaps, stacks, and queues. The array capacity can be automatically adjusted according to the number of elements.
Classification
Indexed arrays---The subscripts are integers, similar to arrays in most languages.
Associative array---The subscript is an unordered and non-repeating key, which is mapped to the corresponding value.
(1) Definition of array
1. Declare the array by direct assignment
Use numbers in square brackets "[]" after the variable name to declare index arrays, and use strings to declare associative arrays.
$Array variable name[index value]=data content //The index value (subscript) can be a string or an integer
When declaring an array variable, you can also use a mixture of numbers and strings in the subscript. But this method is rarely used for one-dimensional arrays
$contact[0]=1
$contact[“id”]=1
$contact[1]="Company A"
$contact["Company"]="Company A"
In the above code, an array $contact is declared, in which a mixture of numbers and strings is used in the subscript. This can be accessed using index or relational methods.
When declaring an index array, if the index value is increasing, you do not need to specify the index value in square brackets. By default, it starts from 0 and increases in sequence. In PHP, the subscript values of the index array can be non-consecutive, as long as the non-consecutive subscript values are specified during initialization.
$contact[]=1; $contact[]=1;
$contact[14]="Gao"; //Specify non-consecutive subscripts as 14
$contact[]="Company A"; //Follow the highest subscript value and add 1 to the subscript to 15
$contact[14]=110; $contact[14]=110; //The element with subscript 14 is reassigned
$contact[]="php"; ’ ’ to 1 to 3 ’ s ’ ‐ off ‐ ‐ ‐ ‐ ‐ ‐ to be 16
print_r($contact); //Array ( [0] => 1 [14] => 110 [15] => Company A [16] => php )
Statement:
All resources on this website are contributed and published by netizens, or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this website are for learning and reference only. Please do not use them for commercial purposes, otherwise you will be responsible for all consequences incurred! If there is any infringement, please contact us to delete and remove it. Contact information: admin@php.cn