Push a Value on an Array

suggest change

There are two ways to push an element to an array: array_push and $array[] =


The array_push is used like this:

$array = [1,2,3];
$newArraySize = array_push($array, 5, 6); // The method returns the new size of the array
print_r($array); // Array is passed by reference, therefore the original array is modified to contain the new elements

This code will print:

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

$array[] = is used like this:

$array = [1,2,3];
$array[] = 5;
$array[] = 6;
print_r($array);

This code will print:

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Executing upon an array:
* Push a Value on an Array

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
89 Executing upon an array
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR