Home >Backend Development >PHP Problem >How to query array value based on array position in php
php method to query array values based on array position: 1. Use the "$array variable name [subscript value]" statement; 2. Use the "array_slice($arr, subscript value, 1)" statement; 3. Use the "array_splice($arr, subscript value, 1)" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php according to the array position Methods to query array values
Method 1: Use the form of $array variable name[subscript value]
<?php $arr = array(10,12,20,25,24); echo $arr[1]; ?>
Method 2: Use array_slice() function
array_slice() function can intercept a value of specified length based on the given array position, and Returns the truncated subarray.
If you just want to get an element, you can set the third parameter of the array_slice() function to 1.
Example: Get the element value with subscript 1
<?php $arr = array(10,12,20,25,24); $result = array_slice($arr,1,1); //截取下标1的元素 var_dump($result); ?>
Method 3: Use the array_splice() function
Similar to the array_slice() function, if you want to get an element, you can set the third parameter of the array_splice() function to 1.
<?php $arr = array(10,12,20,25,24); $result = array_splice($arr,2,1);//截取下标2的元素 var_dump($result); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query array value based on array position in php. For more information, please follow other related articles on the PHP Chinese website!