Can. 5 ways to obtain: 1. Use the "$array name [subscript]" statement to obtain; 2. Use array_shift(), the syntax "array_shift(array)"; 3. Use array_pop() to obtain, the syntax "array_pop(array)" ; 4. Use array_slice() to obtain, the syntax is "array_slice(array, subscript, 1)"; 5. Use array_splice() to obtain, the syntax is "array_splice(array, subscript, 1)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php can get an item of the array value.
Method 1: Use the "$array name[subscript]" statement to obtain
"$array name[subscript]
"You can access an element of the specified subscript in the array
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); var_dump($arr); echo "数组下标为0的一个元素:".$arr[0]."<br>"; echo "数组下标为2的一个元素:".$arr[2]."<br>"; echo "数组下标为3的一个元素:".$arr[3]."<br>"; ?>
Method 2: Use the array_shift() function to obtain
array_shift( ) function will delete the first element in the array and then return the first deleted array element; if the array is empty, NULL will be returned.
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); var_dump($arr); //获取数组中的第一个元素 $first = array_shift($arr); echo "数组的第一个元素为:".$first; ?>
Method 3: Use array_pop() function to obtain
array_pop() function will delete the last element in the array, Then returns the last deleted array element; if the array is empty, NULL will be returned.
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); var_dump($arr); //获取数组中的最后一个元素 $last= array_pop($arr); echo "数组的最后一个元素为:".$last; ?>
Method 4: Use array_slice() function to obtain
array_slice() function is provided by PHP to intercept arrays A function that extracts a fragment from an array.
Use the array_slice() function to obtain the element at the specified position (somewhat similar to method 1)
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); var_dump($arr); echo "截取一个数组元素:"; $result = array_slice($arr,2,1); //截取下标为2的数组元素 var_dump($result); $result = array_slice($arr,1,1); //截取下标为1的数组元素 var_dump($result); $result = array_slice($arr,0,1); //截取下标为0的数组元素 var_dump($result); ?>
Method 5: Use array_splice() Function Get
array_splice() function can when deleting some elements of the array, form these deleted elements into a new array, and then return this new array.
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); var_dump($arr); echo "截取一个数组元素:"; $result = array_splice($arr,2,1); //截取下标为2的数组元素 var_dump($result); ?>
Note: The array_splice() function will change the original array.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Is it possible to get the value of an item in an array in php?. For more information, please follow other related articles on the PHP Chinese website!