Home  >  Article  >  Backend Development  >  How to set numeric subscript in php array

How to set numeric subscript in php array

PHPz
PHPzOriginal
2023-03-23 11:11:581730browse

PHP is a popular open source server-side scripting language used for building dynamic websites. In PHP, an array is a very useful data type used to store collections of data. Array elements can be any type of data, including numbers and strings. In PHP, we can use numeric subscripts to access array elements. Below we will introduce how to set numeric subscripts.

  1. Creation of numerical subscripts

In PHP, the creation of numerical subscripts is very simple. Just enter the number in square brackets. For example:

$arr = array("apple", "banana", "orange");
echo $arr[0]; //输出 "apple"
echo $arr[1]; //输出 "banana"
echo $arr[2]; //输出 "orange"

In this example, we create an array with three elements and access its elements using numeric subscripts 0, 1, 2.

  1. Dynamic setting of numerical subscripts

In addition to using static numerical subscripts, we can also use variables or expressions to dynamically set numbers subscript. For example:

$arr = array("apple", "banana", "orange");
$i = 1;
echo $arr[$i]; //输出 "banana"

In this example, we use the variable $i to set the numeric subscript. Since $i has a value of 1, we will access the second element in the array.

  1. The difference between numeric array subscripts and associative array subscripts

Although we can use numbers and strings as array subscripts, this There is a difference between the two types of subscripts in PHP. Numeric subscripts are arranged sequentially, while associative array subscripts use string names to name array elements.

$arr = array("apple", "banana", "orange"); // 数字下标
$arr = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "orange"); //关联数组下标

In this example, we create two arrays, one using numeric subscripts and the other using associative array subscripts. Using numeric subscripts, $arr[0] represents "apple", $arr[1] represents "banana", and $arr[2] represents "orange". Whereas in an associative array, the elements are named "fruit1", "fruit2" and "fruit3" and you can access them like this:

echo $arr["fruit1"]; //输出 "apple"
echo $arr["fruit2"]; //输出 "banana"
echo $arr["fruit3"]; //输出 "orange"
  1. Array Add New Element

In PHP, we can add new elements using the array subscript syntax. For example:

$arr = array("apple", "banana", "orange");
$arr[3] = "pear";

In this example, we add a new element named "pear" to the array and store it with the numeric subscript 3.

  1. Delete elements from array

In PHP, we can use the unset() function to delete array elements. For example:

$arr = array("apple", "banana", "orange");
unset($arr[1]);

In this example, we have deleted the second element in the array, which is "banana". Now, $arr[1] will output "orange".

Summary

In PHP, numerical subscripts are an important way to access array elements. Knowing how to set numeric subscripts can make working with numeric data collections easier. In PHP, we can also use variables and expressions to dynamically set numerical subscripts. At the same time, we can also use the unset() function to delete array elements.

The above is the detailed content of How to set numeric subscript in php array. 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