Php array pad
From w3cyberlearnings
Contents |
PHP function array_pad
This function inserts array to the specified length with a value.
Syntax array_pad
- array: array to be pad
- length: number of elements in the array
- value: new value element to be inserted.
array_pad(array, length, value);
Example 1
<?php $pet = array("dog", "cat"); $pets = array_pad($pet, 4, "Tommy"); print_r($pets); ?>
Output
Array ( [0] => dog [1] => cat [2] => Tommy [3] => Tommy )
Example 2: negative width
- When the length is negative it adds from the front.
<?php $pet = array("dog", "cat"); $pets = array_pad($pet, -4, "Tommy"); print_r($pets); ?>
Output
Array ( [0] => Tommy [1] => Tommy [2] => dog [3] => cat )
Related Links
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys