array_pad


php128 apg

ALLOWS to get a NEW ARRAY, from a given ARRAY, through the INSERT of the same ELEMENT, to the RIGHT or LEFT, until a certain number of ELEMENTS is reached.





This function pops and returns the new ARRAY:

If $size > 0 the padding will be done on the RIGHT.

If $size < 0 the padding will be done on the LEFT.

In BOTH cases, if the ABSOLUTE VALUE is: EQUAL or LESS than the given ARRAY length, no insertion will occur.

It is possible to add at most 1048576 elements at a time.



<?php

arr array_pad 
arr $array int $size mix $value )

where,

$array The given input ARRAY

$size The new size of the ARRAY

$value The value to be padded

?>

 $array 


The given input array.



 $size 


The new size of given input array.



 $value 


Value to pad if $array is less than $size.



  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - 

       PADDING TO RIGHT

- - - - - - - - - - - - - - - - - - - - - */

echo 'The given ARRAY:<br>';
$var01 = [ 2818 ];
print_r($var01);

$nvar01 count($var01);
echo 
'<br>' $nvar01 ' ELEMENTS<br><br>';

$pad01a 32;
echo 
'Padding TWO ' $pad01a ' on the right.<br>';
$nvar01a $nvar01 2;

echo 
'Number of elements to reach: ' $nvar01a;
echo 
'<br><br>The current ARRAY;<br>';

$var01a array_pad($var01$nvar01a$pad01a);

print_r($var01a);

$nvar01a count($var01a);

echo 
'<br>' $nvar01a ' ELEMENTS<br><br><br>';

$pad01b 18;
echo 
'Padding ONE ' $pad01b ' on the right.<br>';

$nvar01b $nvar01a 1;

echo 
'Number of elements to reach: ' $nvar01b;
echo 
'<br><br>The current ARRAY;<br>';

$var01b array_pad($var01a$nvar01b$pad01b);
print_r($var01b);

$nvar01b count($var01b);

echo 
'<br>' $nvar01b ' ELEMENTS<br><br><br>';


$pad01c 8;
echo 
'Padding ONE ' $pad01c ' on the right.<br>';

$nvar01c $nvar01b 1;
echo 
'Number of elements to reach: ' $nvar01c;
echo 
'<br><br>The current ARRAY;<br>';

$var01c array_pad($var01b$nvar01c$pad01c);

print_r($var01c);

$nvar01d count($var01c);

echo 
'<br>' $nvar01d ' ELEMENTS';

?>

 RESULT   

The given ARRAY:
Array
(
[0] => 2
[1] => 8
[2] => 18
)
3 ELEMENTS

Padding TWO 32 on the right.
Number of ELEMENTS to reach: 5

The current ARRAY;
Array
(
[0] => 2
[1] => 8
[2] => 18
[3] => 32
[4] => 32
)
5 ELEMENTS


Padding ONE 18 on the right.
Number of ELEMENTS to reach: 6

The current ARRAY;
Array
(
[0] => 2
[1] => 8
[2] => 18
[3] => 32
[4] => 32
[5] => 18
)
6 ELEMENTS


Padding ONE 8 on the right.
Number of ELEMENTS to reach: 7

The current ARRAY;
Array
(
[0] => 2
[1] => 8
[2] => 18
[3] => 32
[4] => 32
[5] => 18
[6] => 8
)
7 ELEMENTS


  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - -  

           PADDING TO LEFT

- - - - - - - - - - - - - - - - - - - - - */

echo 'The given ARRAY:<br>';
$var02 = [ 323218];
print_r($var02);

$nvar02 count($var02);
echo 
'<br>' $nvar02 ' ELEMENTS<br><br>';

$pad02a 18;
echo 
'Padding ONE ' $pad02a ' on the left.<br>';
$nvar02a $nvar02 1;
$nvar02a = ($nvar02a * -1);

echo 
'Number of elements to reach: ' $nvar02a;
echo 
'<br><br>The current ARRAY;<br>';

$var02a array_pad($var02$nvar02a$pad02a);

print_r($var02a);

$nvar02a count($var02a);

echo 
'<br>' $nvar02a ' ELEMENTS<br><br><br>';

$pad02b 8;
echo 
'Padding ONE ' $pad02b ' on the left.<br>';

$nvar02b $nvar02a 1;
$nvar02b = ($nvar02b * -1);

echo 
'Number of elements to reach: ' $nvar02b;
echo 
'<br><br>The current ARRAY;<br>';

$var02b array_pad($var02a$nvar02b$pad02b);
print_r($var02b);

$nvar02b count($var02b);

echo 
'<br>' $nvar02b ' ELEMENTS<br><br><br>';


$pad02c 2;
echo 
'Padding ONE ' $pad02c ' on the left.<br>';

$nvar02c $nvar02b 1;
$nvar02c = ($nvar02c * -1);

echo 
'Number of elements to reach: ' $nvar02c;
echo 
'<br><br>The current ARRAY;<br>';

$var02c array_pad($var02b$nvar02c$pad02c);

print_r($var02c);

$nvar02d count($var02c);

echo 
'<br>' $nvar02d ' ELEMENTS';

?>

 RESULT   

The given ARRAY:
Array
(
[0] => 32
[1] => 32
[2] => 18
[3] => 8
)
4 ELEMENTS

Padding ONE 18 on the right.
Number of ELEMENTS to reach: -5

The current ARRAY;
Array
(
[0] => 18
[1] => 32
[2] => 32
[3] => 18
[4] => 8
)
5 ELEMENTS


Padding ONE 8 on the right.
Number of ELEMENTS to reach: -6

The current ARRAY;
Array
(
[0] => 8
[1] => 18
[2] => 32
[3] => 32
[4] => 18
[5] => 8
)
6 ELEMENTS


Padding ONE 2 on the right.
Number of ELEMENTS to reach: -7

The current ARRAY;
Array
(
[0] => 2
[1] => 8
[2] => 18
[3] => 32
[4] => 32
[5] => 18
[6] => 8
)
7 ELEMENTS


  3 EXERCISE   

<?php

$var04 
= array(1, array(234));

print_r($var04);
$nvar04 count($var04);
echo 
'<br>' $nvar04 ' ELEMENTS<br><br>';

$ter04 = array(567);
print_r($ter04);
$nter04 count($ter04);
echo 
'<br>' $nter04 ' ELEMENTS<br><br>';

$nvar04 = ($nvar04 1)*-1;

$fvar04 array_pad($var04$nvar04$ter04);
print_r($fvar04);
$nfvar04 count($fvar04);
echo 
'<br>' $nfvar04 ' ELEMENTS<br><br>';

?>

 RESULT   

Array ( [0] => 1 [1] => Array ( [0] => 2 [1] => 3 [2] => 4 ) )
2 ELEMENTS

Array ( [0] => 5 [1] => 6 [2] => 7 )
3 ELEMENTS

Array ( [0] => Array ( [0] => 5 [1] => 6 [2] => 7 ) [1] => 1 [2] => Array ( [0] => 2 [1] => 3 [2] => 4 ) )
3 ELEMENTS


  4 EXERCISE   

<?php

$var05 
= array(135711);

print_r($var05);

$nvar05 count($var05);

echo 
'<br>' $nvar05 ' ELEMENTS<br><br>';

$nvar05 $nvar05 2;

$ter05 13;

$fvar05 array_pad($var05$nvar05$ter05);

print_r($fvar05);

$nfvar05 count($fvar05);

echo 
'<br>' $nfvar05 ' ELEMENTS<br><br>';

?>

 RESULT   

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 11 )
5 ELEMENTS

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 11 )
5 ELEMENTS


  5 EXERCISE   

<?php

$arr06a 
= [ => "one"
                   
=> "five"=> "seven"
                                  
13 => "thirteen" ];

print_r($arr06a);
$nc count($arr06a);
echo 
'<br>( 1 ) ' $nc ' ELEMENTS <br><br>';

$nc $nc 1;
$ins06b FALSE
$arr06b array_pad($arr06a$nc$ins06b);
print_r($arr06b);
$nc06b count($arr06b);
echo 
'<br>( 2 ) ' $nc06b ' ELEMENTS <br><br>';

$ins06c NULL;
$arr06c array_pad($arr06a$nc$ins06c);
print_r($arr06c);
$nc06c count($arr06c);
echo 
'<br>( 3 ) ' $nc06c ' ELEMENTS <br><br>';

$ins06d = [ "BR" => "Brasil"];
$arr06d array_pad($arr06a$nc$ins06d);
print_r($arr06d);
$nc06d count($arr06d);
echo 
'<br>( 4 ) ' $nc06d ' ELEMENTS <br><br>';

$ins06e imagecreate(1,1);
$arr06e array_pad($arr06a$nc$ins06e);
print_r($arr06e);
$nc06e count($arr06e);
echo 
'<br>( 5 ) ' $nc06e ' ELEMENTS <br><br>';

?>

 RESULT   

In PHP 7.4.x does not display any errors.
In PHP 8.0.x it displays a Fatal error: ... in item (5) due to the type change existing in this item.

Try running on both versions.