InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Changing Array Size in PHP
PHP

Changing Array Size in PHP

InfinityCoder November 19, 2016

You want to modify the size of an array, either by making it larger or smaller than itscurrent size.

Use array_pad() to make an array grow:

1
2
3
4
5
// start at three
$array = array('apple', 'banana', 'coconut');
 
// grow to five
$array = array_pad($array, 5, '');

Now, count($array) is 5, and the last two elements, $array[3] and $array[4], contain the empty string.
To reduce an array, you can use array_splice():

1
2
// no assignment to $array
array_splice($array, 2);

This removes all but the first two elements from $array.

Arrays aren’t a predeclared size in PHP, so you can resize them on the fly. To pad an array, use array_pad().

The first argument is the array to be padded. The next argument is the size and direction you want to pad.

To pad to the right, use a positive integer; to pad to the left, use a negative one. The third argument is the value to be assigned to the newly created entries.

The function returns a modified array and doesn’t alter the original.

Here are some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// make a four-element array with 'dates' to the right
$array = array('apple', 'banana', 'coconut');
$array = array_pad($array, 4, 'dates');
print_r($array);
 
Array
(
    [0] => apple
    [1] => banana
    [2] => coconut
    [3] => dates
)
 
// make a six-element array with 'zucchinis' to the left
$array = array_pad($array, -6, 'zucchini');
print_r($array);
 
Array
(
    [0] => zucchini
    [1] => zucchini
    [2] => apple
    [3] => banana
    [4] => coconut
    [5] => dates
)

Be careful: array_pad($array, 4, ‘dates’) makes sure an $array is at least four elements long; it doesn’t add four new elements.

In this case, if $array was already four elements or larger, array_pad() would return an unaltered $array.
Also, if you declare a value for a fourth element, $array[4]:

1
2
3
$array = array('apple', 'banana', 'coconut');
$array[4] = 'dates';
print_r($array);

you end up with a four-element array with indexes 0, 1, 2, and 4:

1
2
3
4
5
6
7
Array
(
    [0] => apple
    [1] => banana
    [2] => coconut
    [4] => dates
)

PHP essentially turns this into an associative array that happens to have integer keys. The array_splice() function, unlike array_pad(), has the side effect of modifying the original array.

It returns the spliced-out array. That’s why you don’t assign the return value to $array. However, like array_pad(), you can splice from either the right or left.
So calling array_splice() with a value of -2 chops off the last two elements from the end:

1
2
3
4
5
6
7
8
9
10
11
// make a four-element array
$array = array('apple', 'banana', 'coconut', 'dates');
 
// shrink to three elements
array_splice($array, 3);
 
// remove last element, equivalent to array_pop()
array_splice($array, -1);
 
// only remaining fruits are apple and banana
print_r($array);

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Parsing Dates and Times from Strings in PHP
You need to get a date or time in a …

Parsing Dates and Times from Strings in PHP

Stress-Testing Your Website in PHP
You want to find out how well your website performs …

Stress-Testing Your Website in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap