Home  >  Article  >  Backend Development  >  How to delete the first three elements of an array in php

How to delete the first three elements of an array in php

青灯夜游
青灯夜游Original
2022-07-05 20:32:561896browse

Two methods for PHP to delete the first three elements of an array: 1. Use the array_slice() function to intercept all elements starting from the fourth element (after the third element). The syntax "array_slice($arr ,3)”. 2. Use the array_splice() function to intercept all elements starting from the fourth element (after the third element), the syntax is "array_splice($arr,3)".

How to delete the first three elements of an array in php

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

php Two ways to delete the first three elements of the array

In PHP, you can use the array_slice() function or array_splice() function to delete the first three elements by intercepting the array.

Method 1. Use array_slice() function

array_slice() function can intercept the specified number (determined by $length parameter) starting from the specified position (determined by $start parameter) , can be omitted.) Elements

If you want to delete the first three elements of the array, just intercept all elements starting from the fourth element (after the third element); that is, set the parameter $start to 3 .

Example: intercept all elements starting from the fourth element

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

echo "删除数组前三个元素:";
$result = array_slice($arr,3); 
var_dump($result);
?>

How to delete the first three elements of an array in php

##Method 2, use 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 the new array; therefore, the array_splice() function can be used to intercept array fragments.

Same as the array_slice() function, just set the second parameter $start of the function to 3.

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

echo "删除数组前三个元素:";
$result = array_splice($arr,3); 
var_dump($result);
?>

How to delete the first three elements of an array in php

Description: The

array_slice() function is a function provided by PHP to intercept an array. It can 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 ]] )

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.
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to delete the first three elements of an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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