Home > Backend Development > PHP Problem > How to query array value based on array position in php

How to query array value based on array position in php

青灯夜游
Release: 2023-03-12 10:54:01
Original
2385 people have browsed it

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.

How to query array value based on array position in php

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];
?>
Copy after login

How to query array value based on array position in php

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);
?>
Copy after login

How to query array value based on array position in php

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);
?>
Copy after login

How to query array value based on array position in php

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!

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