CKTIS / PHP
array_pad()
Extend an array to the absolute target length using a repeated value.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
array_pad(array $array, int $length, mixed $value): arrayExtend an array to the absolute target length using a repeated value. A positive length adds entries on the right and a negative length adds them on the left. If the source is already long enough, the operation leaves its contents intact. It is useful for aligning short rows to a common width.
Signature
array_pad(array $array, int $length, mixed $value): array
Example output
[[".","A","B"],["A","B"]]
Examples
<?php
$row = ['A','B'];
echo json_encode([array_pad($row, -3, '.'), array_pad($row, 1, '.')]);
Result
[[".","A","B"],["A","B"]]