array_pad() function in php
×


array_pad() function in php

0 346

array_pad() function of PHP is used to add a certain number of items to the existing array with a user define value and return a new array. It is an inbuilt function of PHP.

Syntax:

array_pad ($array,$size,$value);

here,

  • $array is a PHP array.
  • $size is the number of elements to the new array.
  • $value is the value of the new inserted elements.

  • Example 1:

<?php
$arr=array("Apple","Mango","Grapes");
$result=array_pad($arr,6,"pear");
echo "<pre>";
print_r($result);
?>

Output:

array_pad() function in php


    Example 2:

<?php
// array_pad() function with associative array
$arr=array("fruit1"=>"Apple","fruit2"=>"Mango","fruit3"=>"Grapes");
$result=array_pad($arr,6,"pear");
echo "<pre>";
print_r($result);
?>

Output:

array_pad() function in php



Share:


Leave a Reply


Comments
    Waiting for your comments