Home >Backend Development >PHP Problem >Can php add array elements through assignment?

Can php add array elements through assignment?

青灯夜游
青灯夜游Original
2022-09-28 17:02:452650browse

Can. In PHP, you can assign a value to an array in the format of "$array variable name [subscript] = value;" to add array elements; the subscript can be a string, an integer, or empty (that is, not Specify a specific index value). When the subscript is not empty, it cannot be repeated with an existing subscript value. Otherwise, the element value is replaced instead of adding an element. When the subscript is empty, the default is a numeric index, and the default is to increase sequentially from 0 or after the existing index. There are numerical indexes based on increasing order.

Can php add array elements through assignment?

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

php can add array elements through assignment .

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr = [1,2,3];//定义一个数组
var_dump($arr);
$arr["a"]="aa";
$arr["b"]="bb";
echo "赋值后:";
var_dump($arr);
?>

Can php add array elements through assignment?

As you can see, elements will be added to the end of the array.

Explanation:

In php, you can use the form "$array variable name [subscript] = value;" Use the format to assign values ​​to the array to add array elements

And, subscript can be a string, an integer, or empty (that is, no specific index value is specified).

<?php
header("Content-type:text/html;charset=utf-8");
$arr = [1,2,3];//定义一个数组
var_dump($arr);
$arr[4]=44;
$arr["b"]="bb";
echo "赋值后:";
var_dump($arr);
?>

Can php add array elements through assignment?

Note: When the subscript is not empty, it cannot be repeated with an existing subscript value. Otherwise, the element value will not be added, but the element value will be replaced.

<?php
header("Content-type:text/html;charset=utf-8");
$arr = [1,2,3];//定义空数组
var_dump($arr);
$arr[4]=44;
$arr[1]="bb";
var_dump($arr);
?>

Can php add array elements through assignment?

When the subscript is empty, it defaults to a numeric index, and the default starts from 0 and increases sequentially or on the basis of the existing numeric index.

<?php
header("Content-type:text/html;charset=utf-8");
$arr = [1,2,3];//定义空数组
var_dump($arr);
$arr["a"]="aa";
$arr[]="bb";
echo "赋值后:";
var_dump($arr);
?>

Can php add array elements through assignment?

Extended knowledge:

Array (Array) is a linear table data structure. Simply put, it is a set of data collection. 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.

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 (subscript) consists of numbers, starting from 0 by default, and each number corresponds to The position of the array element in the array does not need to be specified.

  • Associative array

    The key name (subscript) is composed of a mixture of numerical values ​​and strings; if a key name in an array is not a number, Then this array is an associative array. As shown below:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array(1,2,3,4,"a"=>"aa");
var_dump($array);//打印数组
?>

Can php add array elements through assignment?

The key name of the 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.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Can php add array elements through assignment?. 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