php array_splice定义和用法
关于php array_splice的用法以前有讲过了,今天主要是详细的讲一下这个函数在应用中的一些详细的具体的情况,有需要的朋友可以参考一下。
array_splice定义和用法
说明
array array_splice ( array &$input , int $offset [, int $length [, array $ replacement ]] )
array_splice() 把 input 数组中由 offset 和 length 指定的单元去掉,如果提供了 replacement 参数,则用 replacement 数组中的单元取代。返回一个包含有被移除单元的数组。注意 input 中的数字键名不被保留。
如果 offset 为正,则从 input 数组中该值指定的偏移量开始移除。如果 offset 为负,则从 input 末尾倒数该值指定的偏移量开始移除。
如果省略 length,则移除数组中从 offset 到结尾的所有部分。如果指定了 length 并且为正值,则移除这么多单元。如果指定了 length 并且为负值,则移除从 offset 到数组末尾倒数 length 为止中间所有的单元。小窍门:当给出了 replacement 时要移除从 offset 到数组末尾所有单元时,用 count($input) 作为 length。
如果给出了 replacement 数组,则被移除的单元被此数组中的单元替代。如果 offset 和 length 的组合结果是不会移除任何值,则 replacement 数组中的单元将被插入到 offset 指定的位置。注意替换数组中的键名不保留。如果用来替换的值只是一个单元,那么不需要给它加上 array(),除非该单元本身就是一个数组。
以下表达式以同样方式修改了 $input: array_splice() 等价表达式 array_push($input, $x, $y) array_splice($input,
代码如下 | 复制代码 |
count($input), 0, array($x, $y)) array_pop($input) array_splice($input, -1) array_shift($input) array_splice($input, 0, 1) array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y)) $input[$x] = $y // 对于键名和偏移量等值的数组 array_splice($input, $x, 1, $y) |
返回一个包含被移除单元的数组。
例子 1
代码如下 | 复制代码 |
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); $a2=array(0=>"Tiger",1=>"Lion"); print_r(array_splice($a1,0,2,$a2)); ?> |
输出:
Array ( [0] => Dog [1] => Cat )例子 3
length 参数设置为 0:
代码如下 | 复制代码 |
$a1=array(0=>"Dog",1=>"Cat"); $a2=array(0=>"Tiger",1=>"Lion"); array_splice($a1,1,0,$a2); print_r($a1); ?> |
输出:
Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )
应用实例
代码如下 | 复制代码 |
$input1 = array("red", "green", "blue", "yellow"); |
//打印剩下的
print_r($input1);
//打印被移走的
print_r($input2);
因为这个函数的第一个参数是地址引用,返回值是被移走的部份。如果你只是想看剩下的。这样写 就可以了。
代码如下 | 复制代码 |
$input = array("red", "green", "blue", "yellow"); array_splice($input, 2); print_r($input); |
===================================================
我把手册的例子加了一个说明。。
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
//从第2个之后开始选,到剩下的全部,选中的移走。
//也就是 "blue", "yellow" 被选中
// $input is now array("red", "green")
array_splice($input, 1, -1);
//从第1个之后开始选,到剩下的全部倒回来一个,选中的移走。
//也就是 "green", "blue",被选中
// $input is now array("red", "yellow")
array_splice($input, 1, count($input), "orange");
//从第1个之后开始选,到剩下的全部,选中的移走,在当前指针位置加一个新值。
//也就是 "green", "blue", "yellow" 被选中
// $input is now array("red", "orange")
array_splice($input, -1, 1, array("black", "maroon"));
//从最后1个之前开始选,往下选1个,选中的移走,在当前指针位置加进一个数组。
//也就是 "yellow" 被选中
// $input is now array("red", "green","blue", "black", "maroon")
array_splice($input, 3, 0, "purple");
//从第3个之后开始选,一个都不选,在当前指针位置插入新值。
//位置就在 "red", "green", "blue" 和 "yellow" 之间
// $input is now array("red", "green", "blue", "purple", "yellow");

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
