php array_splice定義和用法
Jun 13, 2016 am 11:19 AM關(guān)于php array_splice的用法以前有講過了,今天主要是詳細(xì)的講一下這個函數(shù)在應(yīng)用中的一些詳細(xì)的具體的情況,有需要的朋友可以參考一下。 ?
array_splice定義和用法
說明
array array_splice ( array &$input , int $offset [, int $length [, array $ replacement ]] )
array_splice() 把 input 數(shù)組中由 offset 和 length 指定的單元去掉,如果提供了 replacement 參數(shù),則用 replacement 數(shù)組中的單元取代。返回一個包含有被移除單元的數(shù)組。注意 input 中的數(shù)字鍵名不被保留。
如果 offset 為正,則從 input 數(shù)組中該值指定的偏移量開始移除。如果 offset 為負(fù),則從 input 末尾倒數(shù)該值指定的偏移量開始移除。
如果省略 length,則移除數(shù)組中從 offset 到結(jié)尾的所有部分。如果指定了 length 并且為正值,則移除這么多單元。如果指定了 length 并且為負(fù)值,則移除從 offset 到數(shù)組末尾倒數(shù) length 為止中間所有的單元。小竅門:當(dāng)給出了 replacement 時要移除從 offset 到數(shù)組末尾所有單元時,用 count($input) 作為 length。
如果給出了 replacement 數(shù)組,則被移除的單元被此數(shù)組中的單元替代。如果 offset 和 length 的組合結(jié)果是不會移除任何值,則 replacement 數(shù)組中的單元將被插入到 offset 指定的位置。注意替換數(shù)組中的鍵名不保留。如果用來替換的值只是一個單元,那么不需要給它加上 array(),除非該單元本身就是一個數(shù)組。
以下表達(dá)式以同樣方式修改了 $input: array_splice() 等價表達(dá)式 array_push($input, $x, $y)? array_splice($input,
?代碼如下 | 復(fù)制代碼 |
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 // 對于鍵名和偏移量等值的數(shù)組? array_splice($input, $x, 1, $y)? |
返回一個包含被移除單元的數(shù)組。
?
例子 1
?代碼如下 | 復(fù)制代碼 |
$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 參數(shù)設(shè)置為 0:
?代碼如下 | 復(fù)制代碼 |
$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 )
應(yīng)用實(shí)例
?代碼如下 | 復(fù)制代碼 |
$input1 = array("red", "green", "blue", "yellow"); |
//打印剩下的
print_r($input1);
//打印被移走的
print_r($input2);
因?yàn)檫@個函數(shù)的第一個參數(shù)是地址引用,返回值是被移走的部份。如果你只是想看剩下的。這樣寫 就可以了。
?代碼如下 | 復(fù)制代碼 |
$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個之后開始選,到剩下的全部,選中的移走,在當(dāng)前指針位置加一個新值。
//也就是 "green", "blue", "yellow" 被選中
// $input is now array("red", "orange")
array_splice($input, -1, 1, array("black", "maroon"));
//從最后1個之前開始選,往下選1個,選中的移走,在當(dāng)前指針位置加進(jìn)一個數(shù)組。
//也就是 "yellow" 被選中
// $input is now array("red", "green","blue", "black", "maroon")
array_splice($input, 3, 0, "purple");
//從第3個之后開始選,一個都不選,在當(dāng)前指針位置插入新值。
//位置就在 "red", "green", "blue" 和 "yellow" 之間
// $input is now array("red", "green", "blue", "purple", "yellow");

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses
