Home>Article>Backend Development> PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)
Array is a very basic and important data structure. It plays a vital role in various programming languages, and the same is true for PHP language. Mastering the relevant knowledge of PHP arrays is crucial for subsequent PHP learning. In the next time, we will learn about PHP arrays through a series of articles, let's learn together!
What is a PHP array?
First of all, at the beginning of the article, let’s take a brief look atPHP array:
Array is a linear table data structure, which is simply a collection of data. Each member in the array is called an element, and each element is distinguished by a special identifier, which is called a key and is called an array index. (Attachment:PHP function array array function video explanation)
Each entity in the array contains two items, namely key and value. The corresponding array elements can be obtained by key value. These keys can be numeric keys or association keys. The corresponding arrays can be divided into two types:
Index array
The key name consists of numbers, starting from 0 by default. Each number corresponds to an array element in the array. The location in does not need to be specified.
Associative array
The key name is composed of a mixture of numerical values and strings.
Because PHP is a weak language type, array variables in PHP can use a set of continuous memory spaces to store any number of data of any type, and can implement other strong data Functions of data structures such as heap, stack, queue, etc. in the type.
Creation and initialization of PHP arrays
In PHP, there are two ways to create an array: "Directly for array elements Assignment" and "array() function", below we will introduce these two methods in detail with our actual code examples.
1. Directly assign values to array elements
We can use the format of "$array variable name[subscript] = value;
" Format to create and initialize the array
The subscript
can be a string, an integer, or empty (that is, no specific index value is specified).
Example 1:
Output result:
Example 2:
Output result:
It can be seen that when we do not specify a specific index value within square brackets, the default is a numeric index, and the index value increases sequentially starting from 0 by default.
2. Use the array() function
The array() function is used to create an array.
1) Empty array
Direct array() function, without adding any parameters, can create an empty array
Output:
2) Index array
Output:
3) Associative array
array The () function also accepts a certain number ofkey=>value
parameter pairs separated by commas.
"欢迎来到",1=>"php中文网",2=>"PHP教程","URL"=>"//m.sbmmt.com/"); //输出语句 var_dump($array); ?>
Output results:
Recommendation:《2021 PHP Interview Questions Summary (Collection)》《php video tutorial》
The above is the detailed content of PHP array learning how to create and initialize a one-dimensional array (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!