Interception, equal division and replacement of partial arrays in PHP

黄舟
Release: 2023-03-07 12:44:01
Original
6167 people have browsed it

PHP array interception, equal division and replacement of part of the array

In this article, we will introduce the interception of the array (array_slice) and equal division (array_chunk) and replacement (array_splice) and the difference between array_slice and array_splice!

In the previous three articles "How to sort PHP array?" "PHP array random order and reverse order" and "PHP array In "Reverse Order Arrangement", we introduced the sorting of arrays, including ascending order, descending order, disordered order and reverse order of arrays. I believe everyone has a certain understanding of the sorting of arrays. Today we will introduce another group Array functions!

What is an intercepted array? (array_slice)

array_slice takes out a segment of elements from the array. The first parameter is the original array, and the second parameter is the original array. The first parameter is the starting subscript (remember that the array starts from 0), and the third parameter is the number of elements taken from the subscript. If not set, it will be taken until the end of the array by default!

array_slice syntax format is as follows:

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
Copy after login
ParameterDescription
arrayInput array.
offsetIf offset is non-negative, the sequence will start at this offset in array. If offset is negative, the sequence will start this far away from the end in the array.
lengthIf length is given and positive, there will be this many units in the sequence. If length is given and negative, the sequence will terminate this far from the end of the array. If omitted, the sequence will start at offset and go to the end of array.
preserve_keysNote that array_slice() will reorder and reset the numeric index of the array by default. You can change this behavior by setting preserve_keys to TRUE.

具体我们看下面的实例代码:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr=array("PHP中文网",  "m.sbmmt.com",  "百度","搜狗");
$array = array_slice($arr,1,2); //从下标1开始截取,取2个元素,赋给新的变量
print_r($array);
?>
Copy after login

输出的结果为:

Interception, equal division and replacement of partial arrays in PHP

上面的实例示我们指定截取多少个元素的,如果我们不指定呢?也就是array_slice()的第三个参数不写会是什么情况?下面我们一起看下示例代码,还是以上面代码为例:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr=array("PHP中文网",  "m.sbmmt.com",  "百度","搜狗");
$array = array_slice($arr,1); //从下标1开始截取,截取到最后一个元素,赋给新的变量
print_r($array);
?>
Copy after login

输出的结果为:

Interception, equal division and replacement of partial arrays in PHP

看到这大家应该就明白了,当array_slice()的第三个参数不写,那么就会默认截取到数组的最后一个元素!array_slice()函数的第二参数就是指定从哪个下标开始截取!

这里要说明一下:

array_slice仅仅是将数组中的一段取出重新赋值给别的数组,而原数组是不受影响的,也就是说,上面代码数组中的 m.sbmmt.com和百度依然存在的!

什么是替换部分数组?(array_splice)

array_splice() 函数与 array_slice() 函数类似,选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替,换句话说就是去掉数组中的某一部分并用其它值取代。

array_splice语法格式如下:

array array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] )
Copy after login

把 input 数组中由 offset 和 length 指定的单元去掉,如果提供了 replacement参数,则用其中的单元取代。

注意:

1.如果 replacement 不是数组,会被 类型转换 成数组 (例如: (array) $replacement)。 当传入的 replacement 是个对象或者 NULL,会导致未知的行为出现。

2.注意 input 中的数字键名不被保留。

下面我们来看具体的示例代码:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr=array("PHP",  "语言",  "百度","搜狗");
print_r($arr);
echo "<br>";
array_splice($arr,1,2,array("PHP中文网","m.sbmmt.com","php.cn"));
print_r($arr);
?>
Copy after login

输出的结果为:

Interception, equal division and replacement of partial arrays in PHP

从上面的实例中可以看出 数组的“语言”,“百度”被“PHP中文网”,“m.sbmmt.com”,"php.cn"替代换区。

上面的示例中,我们是将替换后将其赋给一个新的数组,那么如果没有新的数组呢?我们看下面的实例:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr=array("PHP",  "语言",  "百度","搜狗");
print_r($arr);
echo "<br>";
array_splice($arr,1,2);
print_r($arr);
?>
Copy after login

输出的结果为:

Interception, equal division and replacement of partial arrays in PHP

从输出的结果中就可以看出,如果没有新的数组,那么就相当于 array_slice,切掉相关位置的数组!

array_splice和array_slice两个函数的相同点和不同点

相同点:

可以实现对数组,进行指定下标位置,和指定元素个数进行数组切割

(其实,就是删除指定的数组元素)

不同点:

array_slice 是传值函数, 原数组不会变化,切割后,可以赋给一个新数组!

array_splice是传址函数,会直接修改原数组,可以设置新的元素,去替换被切割掉的数组元素!

什么是等分数组?(array_chunk)

array_chunk()函数是将数组中的元素数量等分的切割成一个二维数组,其中每个数组的单元数目由第二个参数 size 决定。数组的最后一个单元数目可能会少于 size 个,下面我们一起看下语法格式:

Interception, equal division and replacement of partial arrays in PHP

下面我们直接用实例代码带大家了解:

<?php
header("Content-Type:text/html; charset=utf-8");
$input_array = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;];
$new_array = array_chunk($input_array, 3);
print_r($new_array);
?>
Copy after login

输出结果为:

Interception, equal division and replacement of partial arrays in PHP

在下一篇文章中我们将介绍合并数组的函数,具体详情阅读《PHP数组如何合并?

【相关教程推荐】

1. Relevant topic recommendations: "php array (Array)"


The above is the detailed content of Interception, equal division and replacement of partial arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!

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