Home > Backend Development > PHP Problem > Can PHP add elements to an array without using array functions?

Can PHP add elements to an array without using array functions?

青灯夜游
Release: 2023-03-17 13:30:01
Original
1402 people have browsed it

php can add elements to an array without using array functions. In PHP, you can use the array literal "[]" to add elements to and from the end of the array. The syntax is "$array variable name [subscript] = value", where the subscript (index value) can be a string or an integer; note the following Scaling values ​​are not repeatable and therefore cannot be set to an existing value. When a specific index value (subscript) is not specified within square brackets "[]", the default is a numeric index, and the index value will increase sequentially from the original numeric index.

Can PHP add elements to an array without using array functions?

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

php can be used to add arrays to arrays without using array functions Add elements.

In PHP, we can use the array literal "[]", in the form of "$array variable name[subscript] = value;" format adds elements to and from the end of the array.

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");
$array = array(&#39;Apple&#39; => &#39;苹果&#39;,&#39;Banana&#39; => &#39;香蕉&#39;,&#39;Orange&#39; => &#39;橘子&#39;,&#39;Plum&#39; => &#39;李子&#39;,&#39;Strawberry&#39; => &#39;草莓&#39;);
var_dump($array);
$array[0] = &#39;欢迎&#39;;
$array[1] = &#39;来到&#39;;
$array[2] = &#39;PHP中文网&#39;;
$array[&#39;url&#39;] = &#39;https://www.php.cn/&#39;;
//输出语句
var_dump($array);
?>
Copy after login

Can PHP add elements to an array without using array functions?

Note: The subscript value cannot be repeated. If it is repeated, the element is not added but replaced, so it cannot be set to an existing value.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
var_dump($array);
$array[0] = &#39;欢迎&#39;;
$array[1] = &#39;来到&#39;;
$array[2] = &#39;PHP中文网&#39;;
$array[&#39;url&#39;] = &#39;https://www.php.cn/&#39;;
//输出语句
var_dump($array);
?>
Copy after login

Can PHP add elements to an array without using array functions?

If you are worried that the set subscript already exists (replacement element), it becomes a replacement element; you can not specify a specific index value in square brackets, so the default is Numeric index, the index value will increase sequentially from the original numerical index.

<?php 
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
var_dump($array);
$array[] = &#39;欢迎&#39;;
$array[] = &#39;来到&#39;;
$array[] = &#39;PHP中文网&#39;;
$array[] = &#39;https://www.php.cn/&#39;;
//输出语句
var_dump($array);
?>
Copy after login

Can PHP add elements to an array without using array functions?

The previous subscript is a string, then the index value starts from 0 and increases sequentially

<?php
header("Content-type:text/html;charset=utf-8");
$array = array(&#39;Apple&#39; => &#39;苹果&#39;,&#39;Banana&#39; => &#39;香蕉&#39;,&#39;Orange&#39; => &#39;橘子&#39;,&#39;Plum&#39; => &#39;李子&#39;,&#39;Strawberry&#39; => &#39;草莓&#39;);
var_dump($array);
$array[] = &#39;欢迎&#39;;
$array[] = &#39;来到&#39;;
$array[] = &#39;PHP中文网&#39;;
$array[] = &#39;https://www.php.cn/&#39;;
//输出语句
var_dump($array);
?>
Copy after login

Can PHP add elements to an array without using array functions?

Recommended learning : "PHP Video Tutorial"

The above is the detailed content of Can PHP add elements to an array without using array functions?. 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