Skip to main content
SidsProjectImpact

Back to all posts

How to Push Data Into Array In Php?

Published on
4 min read
How to Push Data Into Array In Php? image

Best PHP Array Manipulation Guides to Buy in August 2026

1 PHP Arrays: Single, Multi-dimensional, Associative and Object Arrays in PHP 7

PHP Arrays: Single, Multi-dimensional, Associative and Object Arrays in PHP 7

BUY & SAVE
$27.99
PHP Arrays: Single, Multi-dimensional, Associative and Object Arrays in PHP 7
2 PHP in easy steps: Updated for PHP 8

PHP in easy steps: Updated for PHP 8

BUY & SAVE
$11.61 $16.99
Save 32%
PHP in easy steps: Updated for PHP 8
3 PHP Beginner's Practical Guide

PHP Beginner's Practical Guide

  • IN-DEPTH PHP & MYSQL COVERAGE WITH PRACTICAL EXAMPLES.
  • EASY-TO-UNDERSTAND HTML INTEGRATED WITH PHP AND MYSQL.
  • ALIGNS WITH B.E./B.TECH SYLLABUS FOR EFFECTIVE TRAINING.
BUY & SAVE
$14.95
PHP Beginner's Practical Guide
4 Beginning PHP 5.3 (Wrox Programmer to Programmer)

Beginning PHP 5.3 (Wrox Programmer to Programmer)

BUY & SAVE
$12.86 $39.99
Save 68%
Beginning PHP 5.3 (Wrox Programmer to Programmer)
5 Beginning PHP and MySQL: From Novice to Professional

Beginning PHP and MySQL: From Novice to Professional

BUY & SAVE
$59.01 $89.99
Save 34%
Beginning PHP and MySQL: From Novice to Professional
6 The New Real Book

The New Real Book

  • AFFORDABLE PRICES FOR QUALITY USED BOOKS IN EXCELLENT SHAPE!
  • ECO-FRIENDLY CHOICE: GIVE BOOKS A SECOND LIFE WHILE SAVING MONEY!
  • RELIABLE SOURCING: EVERY BOOK INSPECTED FOR QUALITY AND CONDITION!
BUY & SAVE
$47.00
The New Real Book
+
ONE MORE?

To push data into an array in PHP, you can use the array_push() function or the assignment operator ([]) to add new elements at the end of the array. Here are the details:

  1. Using array_push() function: The array_push() function accepts the array as the first parameter and the value(s) to be added as subsequent parameters. It appends the value(s) to the end of the array. Here's an example: $fruits = array("apple", "banana"); array_push($fruits, "orange", "grape"); In the above example, the $fruits array initially contains "apple" and "banana". After using array_push(), "orange" and "grape" are added to the array, making it: "apple", "banana", "orange", "grape".
  2. Using assignment operator []: You can also use the assignment operator ([]) to add elements to an array. This method is simpler and more commonly used. Here's an example: $fruits = array("apple", "banana"); $fruits[] = "orange"; $fruits[] = "grape"; In the above example, by assigning new values using [] to the $fruits array, "orange" and "grape" are appended at the end of the array. The result is the same as the previous example: "apple", "banana", "orange", "grape".

Both methods achieve the same result of adding data to an array. You can choose the one that suits your coding style or preference.

How to access array values in PHP?

To access array values in PHP, you can use the following methods:

  1. Square bracket notation: You can access a specific element in an array by specifying its index inside square brackets ([]).

Example:

$fruits = array("apple", "banana", "orange"); echo $fruits[0]; // Output: apple

  1. foreach loop: You can iterate over each element of an array using the foreach loop. It automatically fetches the value of each element and allows you to perform actions on it.

Example:

$fruits = array("apple", "banana", "orange"); foreach($fruits as $fruit) { echo $fruit . "
"; } // Output: // apple // banana // orange

  1. Associative Arrays: If you have an associative array (an array with named keys), you can access its values using the key name inside square brackets ([]).

Example:

$student = array("name" => "John", "age" => 25, "grade" => "A"); echo $student["name"]; // Output: John

Note: Arrays in PHP are zero-indexed, meaning the first element has an index of 0.

What is the syntax to add elements to an array in PHP?

In PHP, you can add elements to an array using the array_push() function or by directly assigning a value to a specific index. Here are examples of both approaches:

Using array_push():

$myArray = array(); // or $myArray = [];

array_push($myArray, "element1", "element2", "element3");

print_r($myArray); // Output: Array ( [0] => element1 [1] => element2 [2] => element3 )

Direct assignment:

$myArray = array(); // or $myArray = [];

$myArray[] = "element1"; $myArray[] = "element2"; $myArray[] = "element3";

print_r($myArray); // Output: Array ( [0] => element1 [1] => element2 [2] => element3 )

Both methods achieve the same result of adding elements to the end of the array.

How to push data into an array in PHP?

To push data into an array in PHP, you can use the array_push() function or directly assign a new value to a new index of the array.

Here are two ways to push data into an array:

  1. Using array_push() function:

$myArray = []; // Initialize an empty array

// Pushing data into the array array_push($myArray, "data1"); array_push($myArray, "data2"); array_push($myArray, "data3");

  1. Directly assigning a new value to a new index of the array:

$myArray = []; // Initialize an empty array

// Pushing data into the array $myArray[] = "data1"; $myArray[] = "data2"; $myArray[] = "data3";

In both cases, you start with an empty array $myArray and then use either array_push() or the shorthand notation $myArray[] to push data into the array. The resulting array will contain the pushed values.