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, ∼4719🔥, 0💬
Popular Posts:
How to add images to my EPUB books Images can be added into book content using the XHTML "img" eleme...
How to how to use matched string and groups in replacements with re.sub()? When calling the re.sub()...
Where Is the Submitted Form Data Stored in PHP? When a user submit a form on your Web server, user e...
How to enter & sign in '@(...)' expressions? & signs can be entered in '@(...)' expr...
Where to find tutorials on RSS specifications? I want to learn it to describe my API services. Here ...