How to remove the first digits of an array in php

青灯夜游
Release: 2023-03-17 18:42:02
Original
1206 people have browsed it

php method to remove the first N digits of an array: 1. Use the array_slice() function. You only need to set the second parameter of the function to N. The syntax is "array_slice($arr,N);" ; 2. To use the array_splice() function, just set the second parameter of the function to 0, and set the third parameter start to "N-array length". The syntax is "array_splice($arr,0,(N -array length));".

How to remove the first digits of an array in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php intercepts the last few digits of the array Element

1. Use array_slice() function

array_slice() function is a function provided by PHP to intercept arrays. It can be obtained from Extract a fragment from the 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.

If you want to use the array_slice() function to delete the first N elements of the array, you only need to set the second parameter of the function to N.

Example: Remove the first 5 digits of the array

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24,22,34,56,78,90);
echo "原数组:";
var_dump($arr);
echo "去掉数组前5位:" ;
$result = array_slice($arr,5); 
var_dump($result);

?>
Copy after login

Output result

How to remove the first digits of an array in php

2 , use the array_splice() function

When the array_splice() function deletes some elements of the array, it will form these deleted elements into a new array, and then return this new array; therefore array_splice() Functions can be used to intercept array fragments.

Just set the second parameter of the array_splice() function to 0, and set the third parameter start to a negative value (-N) to intercept the last N elements of the array; in other words, the third The first parameter start is set to (N-array length) to remove the first N bits of the array.

Example

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

?>
Copy after login

Output result

How to remove the first digits of an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the first 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