How to Use array_push() Function in PHP?

03-Apr-2023

.

Admin

How to Use array_push() Function in PHP?

Hi Dev,

This tutorial is focused on how to use array_push() function in PHP. you'll learn how to add elements to an array in PHP. We will use how to add elements to an array using the array_push() function in PHP. if you have a question about how to use the array_push() function using PHP then I will give a simple example with a solution.

Now, let's see the article on how to add an element to an array using the array_push() function in PHP. it's a simple example of how to use the array_push() function in PHP. you can understand the concept of how to add elements to an array in PHP. if you have a question about how to use the array_push() function using PHP then I will give a simple example with a solution.

Example 1: Add values in array PHP


<?php

$array = array("php", "laravel", "codeigniter");

//Before adding new values

echo "Before adding the value:- ";

print_r($array); echo "<br>";

//Add elements/values in the array

array_push($array,"wordpress","bootstrap","html");

//After adding new values

echo "After adding the value:- ";

print_r($array);

?>

Example 2: PHP array push with key

<?php

$array = array("a"=>"red","b"=>"green");

//Before adding new values

echo "Before adding the value:- ";

print_r($array);

echo "<br>";

//Add the values in an array without using the array function

$array['c'] = "yello";

$array['d'] = "brown";

//After adding new values

echo "After adding the value:- ";

print_r($array);

?>

I hope it could help you...

#PHP