This article mainly introduces the PHP array_slice and Array_splice functions, interested can be onlookers,
The Array_slice and Array_splice functions are used to remove a slice of an array, and Array_splice also use new slices to replace the original deleted slice location. Similar to the Array.prototype.splice and Array.prototype.slice methods in JavaScript.
Array_slice
Array array_slice (array $array, int $offset [, int $length = NULL [, bool $preserve _keys = false]])
Returns a slice of the array that specifies the subscript offset and length of the sub array.
Parameter description
Sets the length of the first argument array to num_in.
Offset
If offset is a positive number and is less than length, the returned array begins with offset, and if offset is greater than length, it is not manipulated and returned directly. If offset is a negative number, offset = Num_in+offset, and if Num_in+offset = 0, the offset is set to 0.
Length
If length is less than 0, then length is converted to Num_in-offset + length; otherwise, length = Num_in-offset if Offset+length > Array_count. If the length is still less than 0 after processing, it is returned directly.
Preserve_keys
The default is False, and the original order of numeric key values is not preserved by default, and the original numeric value order of the array is preserved when set to true.
Working with instances
<?php
$input = Array ("A", "B", "C", "D", "E");
$output = Array_slice ($input, 2); Returns "C", "D", and "E"
$output = Array_slice ($input,-2, 1);//returns "D"
$output = array_slice ($input, 0, 3); Returns "A", "B", and "C"
Print_r (The Array_slice ($input, 2,-1));//Array (0 => ' C ', 1 => ' d ');
Print_r (Array_slice ($input, 2,-1, true)); Array (2 => ' C ', 1 => ' d ');
Run steps
- Processing parameters: Offset, length
- Move the pointer to the location where the offset points
- Start with offset, copy length elements to return array
Run the flowchart below
Array_splice
Array Array_splice (array & $input, int $offset [, int $length = 0 [, Mixed $replacement = Array ()]])
Deletes the length element from offset in input and replaces the deleted element with the replacement array if there is a replacement parameter.
Parameter description
The offset and length parameters in the Array_splice function are the same as those in the Array_slice function.
Replacement
- If this parameter is set, then the function will be replaced with the replacement array.
- If offset and length specify that no elements need to be removed, then replacement is inserted into the offset position.
- If replacement has only one element, it can be wrapped without an array ().
Using the sample
<?php
$input = Array ("Red", "green", "blue", "yellow");
Array_splice ($input, 2);
$input into Array ("Red", "green")
$input = Array ("Red", "green", "blue", "yellow");
Array_splice ($input, 1,-1);
$input into Array ("Red", "yellow")
$input = Array ("Red", "green", "blue", "yellow");
Array_splice ($input, 1, Count ($input), "Orange");
$input into Array ("Red", "orange")
$input = Array ("Red", "green", "blue", "yellow");
Array_splice ($input,-1, 1, Array ("Black", "maroon"));
The $input is array ("Red", "green",
// "Blue", "Black", "maroon")
$input = Array ("Red", "green", "Blue", " Yellow ");
Array_splice ($input, 3, 0, "purple");
$input Array ("Red", "green",
// "blue", "purple", "yellow");
Source Code Interpretation
In Array_splice, there's this piece of code:
/* Don T create the array of removed elements if it isn't going * to be
used e.g removing Ents
/if (return_value_used) {//if useful to function return value, create return array, do not create return array
int size = length;
/* Clamp the offset ... */
if (Offset > num_in) {
offset = num_in
} else if (offset < 0 && (offset = (num_in + offset)) < 0) {
offset = 0;
}
/* .. and the length */
if (length < 0) {
size = num_in-offset + length;
} else if (((unsigned long) offset + (unsigned long) length) > (unsigned) num_in) {
size = Num_in-offset;
}
/* Initialize return value *
/array_init_size (return_value, size > 0? size:0);
Rem_hash = &z_arrval_p (Return_value);
}
The Array_splice function returns the slices that were deleted. This code means that if the array_splice needs to return a value, then the return array is created, otherwise it is not created, so that space is not wasted. This is also a small programming technique that comes back only when it is needed. For example, if you use $result = Array_splice (...) in a function, then return_value_used is true.
Summarize
At the end of this article, in peacetime programming, should be like these two functions to implement the same way, the most special situation to deal with the first, and then continue to avoid making redundant judgments, there is a need to save new variables to apply for new space, otherwise it will cause waste.
Thank you for reading, I hope to help you, thank you for your support for this site!