W3docs

Add data dynamically to an Array

To add data dynamically to an array in PHP, you can use the $array[] = $value syntax.

To add data dynamically to an array in PHP, you can use the $array[] = $value syntax. For example:

Example of adding data dynamically to an Array in PHP

php— editable, runs on the server

This will add the elements 'apple', 'banana', and 'orange' to the array $array.

You can also use the array_push() function to add elements to the end of an array:

Example of using array_push() function to add elements to the end of an array in PHP

php— editable, runs on the server

This will have the same effect as the previous example.

If you want to add an element to the beginning of an array, you can use the array_unshift() function:

Example of using array_unshift()) function to add an element to the beginning of an array in PHP

php— editable, runs on the server

This will add the elements 'orange', 'banana', and 'apple' to the array $array, in that order.