Skip to main content

PHP array_pad() Function Tutorial with Examples

 

📌 PHP array_pad() Function Tutorial with Examples

The array_pad() function in PHP is used to pad an array to a specified length with a specified value. If the array has fewer elements than the target size, it fills the rest with the value you provide.


📌 Syntax:


array_pad(array $array, int $size, mixed $value): array

Parameters:

  • $array → The original array you want to pad.

  • $size → The target size of the new array.

    • Positive value: Pads to the end (right).

    • Negative value: Pads to the beginning (left).

  • $value → The value to use for padding.


📌 How it Works:

  • If the array is already equal to or larger than the desired size, no padding is added.

  • If it's smaller, the function adds the specified value until the array reaches the target size.

  • It returns a new array — the original remains unchanged.


📌 Example 1: Pad Array to the Right


<?php $fruits = array("Apple", "Banana"); $result = array_pad($fruits, 5, "Fruit"); print_r($result); ?>

Output:


Array ( [0] => Apple [1] => Banana [2] => Fruit [3] => Fruit [4] => Fruit )

📌 Example 2: Pad Array to the Left


<?php $numbers = array(10, 20, 30); $result = array_pad($numbers, -5, 0); print_r($result); ?>

Output:


Array ( [0] => 0 [1] => 0 [2] => 10 [3] => 20 [4] => 30 )

📌 Example 3: No Padding Needed


<?php $colors = array("Red", "Green", "Blue", "Yellow"); $result = array_pad($colors, 3, "Black"); print_r($result); ?>

Output:


Array ( [0] => Red [1] => Green [2] => Blue [3] => Yellow )

📌 Use Cases:

✅ Ensuring an array has a minimum length
✅ Filling empty positions in an array with placeholder values
✅ Creating fixed-length arrays for layouts or calculations


📌 Final Thoughts:

The array_pad() function is a handy tool for adjusting array sizes and filling gaps with any default value. It’s particularly useful for working with form data, table rows, or fixed-size structures where you need to guarantee a consistent number of elements.


📌 Official Documentation:

For more details, visit:
👉 PHP array_pad() Documentation

Comments

Popular posts from this blog

PHP array_column() Function

  <?php // An array that represents a possible record set returned from a database $a =  array (    array (      'id'  =>  5698 ,      'first_name'  =>  'Mehran' ,      'last_name'  =>  'Yaqoob' ,   ),    array (      'id'  =>  4767 ,      'first_name'  =>  'Muneeb' ,      'last_name'  =>  'Ahmad' ,   ),    array (      'id'  =>  3809 ,      'first_name'  =>  'Uzair' ,      'last_name'  =>  'Rajput' ,   ) ); $last_names = array_column($a,  'last_name' ); print_r($last_names); ?> Output: Array (   [0] => Yaqoob   [1] => Ahmad   [2] => Rajput ) Syntex: array_column( array ,  column_key ,  index_key ...

Mastering PHP's array_merge(): When to Use It (And When Not To)

Pros, Cons, and Best Practices PHP's  array_merge()  function is one of the most commonly used array functions in web development. Whether you're building a simple website or a complex web application, understanding how to effectively merge arrays can save you time and prevent headaches. In this comprehensive guide, we'll explore everything you need to know about  array_merge() , including its advantages, limitations, practical use cases, and alternatives. What is  array_merge()  in PHP? array_merge()  is a built-in PHP function that combines two or more arrays into a single array. The function takes multiple array arguments and returns a new array containing all the elements from the input arrays. Basic Syntax php array_merge ( array ... $arrays ) : array Simple Example php $array1 = [ 'a' , 'b' , 'c' ] ; $array2 = [ 'd' , 'e' , 'f' ] ; $result = array_merge ( $array1 , $array2 ) ; print_r ( $result ) ; /* O...