Home > Backend Development > PHP Problem > How to intercept the last few digits of an array in php

How to intercept the last few digits of an array in php

青灯夜游
Release: 2023-03-13 07:22:01
Original
4307 people have browsed it

php method to intercept the last few elements of the array: 1. Use the "array_slice($array,$start)" statement, and the parameter "$start" must be a negative value; 2. Use "array_splice($array, $start)" statement, the parameter "$start" must be a negative value.

How to intercept the last few digits of an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

After php intercepts the array How many elements

1. Use array_slice() function

array_slice() function is a function provided by PHP to intercept arrays. A fragment can be extracted from an array. The syntax is as follows:

array array_slice ( array $arr , int $start [, int $length = NULL [, bool $preserve_keys = false ]] )
Copy after login

Parameter description:

  • arr represents the array to be intercepted.
  • start indicates the starting position (subscript) of interception:
    • If start is a positive number, interception will be performed from front to back.
    • If start is a negative number, start from the position -start from the end of arr and intercept from back to front. For example -2 means start from the second to last element of the array.
  • length is an optional parameter, indicating the intercepted length:
    • If length is a positive number, it indicates the number of intercepted elements;
    • If length If it is a negative number, then the intercepted fragment will end at the length position from the end of the array;
    • If it is omitted, it will start from the start position and continue to the end of the array.
  • preserve_keys is an optional parameter that specifies whether to retain the original key name. The default is false, that is, not retained; if set to true, the original key name will be retained.

Example: intercept the last few elements of the array

If you want to intercept the last few elements of the array, just set the parameter start to a negative value

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,12,20,25,24);
echo "原数组:";
var_dump($arr); 

echo "截取数组后2位的元素片段:";
$result = array_slice($arr,-2); //截取数组后2位的元素
var_dump($result);


?>
Copy after login

Output result

How to intercept the last few digits of an array in php

2. Use the array_splice() function

array_splice() function to delete the array When a part of the elements is removed, these deleted elements will be formed into a new array, and then the new array will be returned; therefore, the array_splice() function can be used to intercept array fragments.

Same as the array_slice() function, you only need to set the second parameter start of the function to a negative value to intercept the last few elements of the array.

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,12,20,25,24);
echo "原数组:";
var_dump($arr); 

echo "截取数组后3位的元素片段:";
$result = array_splice($arr,-3); //截取数组后3位的元素
var_dump($result);


?>
Copy after login

Output results

How to intercept the last few digits of an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to intercept the last few digits of an array 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