Tools, FAQ, Tutorials:
Truncating an Array in PHP
How To Truncate an Array in PHP?
✍: FYIcenter.com
If you want to remove a chunk of values from an array, you can use the array_splice($array, $offset, $length) function. $offset defines the starting position of the chunk to be removed. If $offset is positive, it is counted from the beginning of the array. If negative, it is counted from the end of the array. array_splice() also returns the removed chunk of values. Here is a PHP script on how to use array_splice():
<?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
$removed = array_splice($array, -2, 2);
print("Remaining chunk:\n");
print_r($array);
print("\n");
print("Removed chunk:\n");
print_r($removed);
?>
This script will print:
Remaining chunk:
Array
(
[Zero] => PHP
)
Removed chunk:
Array
(
[One] => Perl
[Two] => Java
)
⇒ Joining Array Values into a Single String in PHP
⇐ Padding an Array with a Given Value in PHP
2017-01-05, ∼4789🔥, 0💬
Popular Posts:
Can Multiple Paragraphs Be Included in a List Item? Yes. You can include multiple paragraphs in a si...
How to make application release build with Visual Studio 2017? If you want to make a final release b...
Why am I getting this "Docker failed to initialize" error? After installing the latest version of Do...
How to reinstall npm with a node version manager? I am getting permission errors with the current ve...
How To Avoid the Undefined Index Error in PHP? If you don't want your PHP page to give out errors as...