Home  >  Article  >  Backend Development  >  PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

青灯夜游
青灯夜游Original
2021-07-28 11:21:194637browse

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!

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

What is a PHP array?

First of all, at the beginning of the article, let’s take a brief look at PHP 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:

<?php
header("Content-type:text/html;charset=utf-8");
$array[0] = &#39;欢迎&#39;;
$array[1] = &#39;来到&#39;;
$array[2] = &#39;PHP中文网&#39;;
$array[&#39;url&#39;] = &#39;//m.sbmmt.com/&#39;;
//输出语句
var_dump($array);
?>

Output result:

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$array[] = &#39;香蕉&#39;;
$array[] = &#39;苹果&#39;;
$array[] = &#39;橘子&#39;;
$array[] = &#39;榴莲&#39;;
//输出语句
var_dump($array);
?>

Output result:

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

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

<?php
$arr = array();
//输出语句
var_dump($arr);
?>

Output:

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

2) Index array

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
//输出语句
var_dump($array);
?>

Output:

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

3) Associative array

array The () function also accepts a certain number of key=>value parameter pairs separated by commas.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(0=>"欢迎来到",1=>"php中文网",2=>"PHP教程","URL"=>"//m.sbmmt.com/");
//输出语句
var_dump($array);
?>

Output results:

PHP array learning how to create and initialize a one-dimensional array (detailed code explanation)

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!

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