Home  >  Article  >  Backend Development  >  How many to take out from php array

How many to take out from php array

WBOY
WBOYOriginal
2023-05-05 20:28:05357browse

Arrays are one of the most commonly used data types when developing PHP applications. An array is a container used to hold a set of related data. The elements in the array can be easily accessed, added, deleted, and modified. However, when working with large arrays, only a portion of the data needs to be retrieved without using the entire array.

In this article, we will explore how to take out several array elements in PHP.

  1. array_slice() function

array_slice() is one of the PHP built-in functions that can remove a portion of elements from an array. The usage of this function is as follows:

array array_slice ( array $array , int $offset , int $length = NULL , bool $preserve_keys = FALSE )

Among them, the parameter meaning is as follows:

  • $array: the array that needs to be sliced.
  • $offset: The starting position of the slice, that is, the position from which to start cutting in the original array. If the value is negative, it means starting from the nth element from the bottom. For example, -1 means starting from the last element.
  • $length: Optional, indicating the number of elements to be taken out. If this parameter is a positive number, it means that this number of elements will be filled into the new array starting from offset. If this parameter is a negative number, it means that this number of elements will be filled into the new array starting from $offset until the end. If this parameter is NULL, all elements from $offset to the end of the array are removed.
  • $preserve_keys: Optional, Boolean type, indicating whether to retain the key names in the original array. The default value is false, which means the key name is not preserved.

Sample code:

//定义一个简单的数组
$arr = array('a', 'b', 'c', 'd', 'e', 'f', 'g');

//从索引3处开始往后取3个元素
$result1 = array_slice($arr, 3, 3);  
print_r($result1);  //输出 ["d","e","f"]

//从倒数第三个元素开始往后取3个元素
$result2 = array_slice($arr, -3, 3);
print_r($result2);  //输出 ["e","f","g"]

//取出从索引1处到数组末尾的所有元素
$result3 = array_slice($arr,1);
print_r($result3);  //输出 ["b","c","d","e","f","g"]
  1. array_splice() Function

array_splice() is another PHP built-in function for operating on arrays, The difference from array_slice() is that it changes the original array. The usage of this function is as follows:

array array_splice ( array &$input , int $offset , int $length = 0 , mixed $replacement = array() )

Among them, the parameter meaning is as follows:

  • &$input: The original array that needs to be modified by slicing.
  • $offset: The starting position of the slice, that is, the position from which to start cutting in the original array. If the value is negative, it means starting from the nth element from the bottom. For example, -1 means starting from the last element.
  • $length: Optional, indicating the number of elements to be taken out. If this parameter is a positive number, it means that this number of elements will be filled into the new array starting from $offset, and the original array will be deleted at the same time. If this parameter is a negative number, it means that this number of elements will be filled into the new array starting from $offset until the end. If this parameter is not set, all elements starting from $offset will be deleted directly.
  • $replacement: Optional, indicating an array used to replace deleted elements. If this parameter is not set, the deleted element will be cleared.

Sample code:

//定义一个简单的数组
$arr = array('a', 'b', 'c', 'd', 'e', 'f', 'g');

//从索引3处开始往后取3个元素,并将原数组修改为 ["a","b","c","g"]
array_splice($arr, 3, 3);
print_r($arr);   //输出 ["a","b","c","g"]

Summary

When dealing with large arrays, only a portion of the data needs to be obtained without using the entire array. PHP provides two built-in functions for processing arrays, array_slice() and array_splice(). The former is used to take out part of the data from the array without changing the original array; the latter is used to take out part of the data from the array without changing the original array. We can choose the appropriate function according to specific needs.

The above is the detailed content of How many to take out from php array. 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