Verification: a143cc29221c9be0

Php add all array elements

Reading array elements

To access an element within an array, you write the array’s variable name, followed by the index of the element in square brackets:


$myArray[index]

The following example displays the value of the 3rd element in an array (remember that array indices start from zero):


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );

// Displays "Martin Scorsese"
echo $directors[2];

Associative arrays work in the same way, except that you use a string index instead of a numeric one:


$movie = array( "title" => "Rear Window",
                "director" => "Alfred Hitchcock",
                "year" => 1954 );

// Displays "Rear Window"
echo $movie["title"];

Changing element values

You assign a new value to an array element in the same way as a regular variable:


$myArray[index] = new value;

The following example changes the 3rd element of the array from “Martin Scorsese” to “Woody Allen”:


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
$directors[2] = "Woody Allen";

Adding elements

You can add a new element to an array like this:


$myArray[] = value;

PHP automatically assigns the next available numeric index to the new element. Here’s an example:


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
$directors[] = "Woody Allen";

// Displays "Woody Allen"
echo $directors[4];

You can also specify an index. If an element with that index already exists in the array, its value is overwritten; otherwise a new element is created with that index:


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
$directors[4] = "Woody Allen";

// Displays "Woody Allen"
echo $directors[4];

$directors[2] = "Ingmar Bergman";

// Displays "Ingmar Bergman"
echo $directors[2];

Removing elements

To remove an element from an array, you call unset(), passing in the element to remove. (You can also use unset() on regular variables to delete them.) Here’s an example:


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );

// Displays "Stanley Kubrick"
echo $directors[1];

unset( $directors[1] );

// Displays nothing (and generates an "Undefined offset" notice)
echo $directors[1];

The last line of code above generates an “Undefined offset” notice, which is a minor error. It means that you’re trying to access an array index that doesn’t exist. Normally, PHP doesn’t display or log notices, but you can change this with the error_reporting() function.

unset() doesn’t automatically reindex arrays to “close the gap” in the index numbering. For example, after running the above code, "Martin Scorsese" still has an index of 2, and "Fritz Lang" still has an index of 3. To reindex the array so that the indices are contiguous again, you can use the array_values() function. For example:


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );

// Displays "Stanley Kubrick"
echo $directors[1] . "
"; unset( $directors[1] ); $directors = array_values( $directors ); // Displays "Martin Scorsese" echo $directors[1] . "
";

Outputting an array with print_r()

Sometimes when debugging your code it’s useful to inspect the entire contents of an array. However, using print() or echo() on an array isn’t much help:


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );

// Displays "Array"
echo $directors;

To display an entire array, use the print_r() function instead. Here’s an example that creates an indexed array and an associative array, then displays the contents of both arrays using print_r():


$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );

$movie = array( "title" => "Rear Window",
                "director" => "Alfred Hitchcock",
                "year" => 1954 );

echo '

$directors:

';
print_r( $directors );
echo '

$movie:

';
print_r( $movie );
echo "
";

The above code produces the following output:


$directors:

Array
(
    [0] => Alfred Hitchcock
    [1] => Stanley Kubrick
    [2] => Martin Scorsese
    [3] => Fritz Lang
)

$movie:

Array
(
    [title] => Rear Window
    [director] => Alfred Hitchcock
    [year] => 1954
)

To return the output from print_r() as a string instead of displaying it, pass a second argument with a value of true to print_r().

Use the array_push() Function to Add Elements to an Array in PHP

One way to add elements to an array is to use the array_push function. First, we will create an array using the array() function. After that, we will add elements to that array using the given command. The function array_push() adds elements to the array just like a stack; the correct syntax to execute this is as follows:

array_push($array, $value1, $value2, ..., $valueN);

The built-in function array_push() has N+1 parameters; N is the number of values we want to add to an array. The details of its parameters are as follows:

Parameters Description
$array mandatory It’s the array where we’re adding values to.
$value1, $value2, $valueN mandatory These are the values that we’re adding to an array; it can be a string, integer, float, etc.

This function returns an integer value, which shows the number of elements added to the array. The program below presents how we can use the array_push() function to add elements to an array in PHP.

We have passed 5 values as elements to add to the array.

Output:

The array is empty, as you can see. 
Array
(
)

Now, we have added the values. 

Array
(
    [0] => Rose
    [1] => Jasmine
    [2] => Lili
    [3] => Hibiscus
    [4] => Tulip
)

Now, we check the return value of the function, following the syntax:

Output:

The array is empty, as you can see. 
Array
(
)
Now, we have added the values. 
5
Array
(
    [0] => Rose
    [1] => Jasmine
    [2] => Lili
    [3] => Hibiscus
    [4] => Tulip
)

The output shows the value 5, which is the number of the elements added to the array.

Use the Direct Assignment Method to Add Elements to an Array in PHP

In PHP, we can also use the Direct Assignment Method to add elements to an array. We will directly assign values to an array just like we assign values to an integer or string; the correct syntax to execute this is as follows:

$array[] = $value;

The program that applies this method to add elements to an array is as follows:

Output:

The array is empty, as you can see. 
Array
(
)
Now, we have added the values. 
Array
(
    [0] => Rose
    [1] => Jasmine
    [2] => Lili
    [3] => Hibiscus
    [4] => Tulip
)

Use the array_unshift Function to Add Elements to an Empty Array in PHP

In PHP, we can also use the array_unshift() function to add elements to an array. This function adds values at the beginning of the array. The correct syntax to assign a value to an array is as follows:

array_unshift($array, $value1, $value2, ..., $valueN);

The built-in function array_unshift() has N+1 parameters. The details of its parameters are as follows

Parameters Description
$array mandatory It is the array in which we want to add the values.
$value1, $value2, $valueN mandatory It is the value or values that we want to add. At least one value is mandatory.

The program that applies this function to add elements to an array is as follows:

Output:

The array is empty as you can see. 
Array
(
)
Now we have added the values. 
5
Array
(
    [0] => Rose
    [1] => Jasmine
    [2] => Lili
    [3] => Hibiscus
    [4] => Tulip
)

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.