Tools, FAQ, Tutorials:
Padding an Array with a Given Value in PHP
How To Pad an Array with the Same Value Multiple Times in PHP?
✍: FYIcenter.com
If you want to add the same value multiple times to the end or beginning of an array, you can use the array_pad($array, $new_size, $value) function. If the second argument, $new_size, is positive, it will pad to the end of the array. If negative, it will pad to the beginning of the array. If the absolute value of $new_size if not greater than the current size of the array, no padding takes place. Here is a PHP script on how to use array_pad():
<?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
$array = array_pad($array, 6, ">>");
$array = array_pad($array, -8, "---");
print("Padded:\n");
print(join(",", array_values($array)));
print("\n");
?>
This script will print:
Padded: ---,---,PHP,Perl,Java,>>,>>,>>
⇐ Creating an Array with a Sequence in PHP
2017-01-05, ∼3977🔥, 0💬
Popular Posts:
How To Avoid the Undefined Index Error in PHP? If you don't want your PHP page to give out errors as...
How to start Visual Studio Command Prompt? I have Visual Studio 2017 Community version with Visual C...
How to read RSS validation errors at w3.org? If your RSS feed has errors, the RSS validator at w3.or...
How to add request query string Parameters to my Azure API operation 2017 version to make it more us...
How To Truncate an Array in PHP? If you want to remove a chunk of values from an array, you can use ...