Best way to initialize empty array in PHP
Last Updated :
31 Jul, 2021
Arrays in PHP: Use
array() Function to create an array in PHP. There are three types of array supported in PHP:
- Indexed arrays: Arrays having a numeric index.
- Associative arrays: Arrays having named keys.
- Multidimensional arrays: It contains one or more array in particular array.
Note: Why it is always good practice to declare an empty array and then push the items to that array?
When declare an empty array and then start entering elements in it later. With the help of this, it can prevent different errors due to a faulty array. It helps to have the information of using bugged, rather having the array. It saves time during the debugging. Most of the time it may not have anything to add to the array at the point of creation.
Syntax to create an empty array:
$emptyArray = [];
$emptyArray = array();
$emptyArray = (array) null;
While push an element to the array it can use $emptyArray[] = "first". At this time, $emptyArray contains "first", with this command and sending "first" to the array which is declared empty at starting.
In other words, the initialization of new array is faster, use syntax
var first = [] rather while using syntax
var first = new Array(). The fact is being a constructor function the function
Array() and the,
[] is a part of the literal grammar of array. Both are complete and executed in completely different ways. Both are optimized and not bothered by the overhead of any of the calling functions.
Basic example of empty array:
php
<?php
$emptyArray = (array) null;
var_dump($emptyArray);
?>
Now PHP 5.4, which supports [] as an alternative, As per the compiler concerned it was synonymous and most of the PHP developers uses $array = [] as it makes going back and forth between JS and PHP easier.
php
<?php
/* method to create Empty array. */
$firstempty = [];
echo "Created First empty array <br>";
/* method to create Second Empty array. */
$second = array( );
echo "Created second empty array<br>";
/* First method to create array. */
$first = array( 1, 2);
foreach( $first as $value ) {
echo "Value is $value <br>";
}
/* Second method to create array. */
$first[0] = "one";
$first[1] = "two";
foreach( $first as $value ) {
echo "Value is $value <br>";
}
?>
Output:
Created First empty array
Created second empty array
Value is 1
Value is 2
Value is one
Value is two
Another Method:
php
<?php
// Create an empty array
$emptyArray=array();
// Push elements to the array
array_push($emptyArray, "geeks", "for", "geeks");
// Display array elements
print_r($emptyArray);
?>
Output:
Array
(
[0] => geeks
[1] => for
[2] => geeks
)
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
How to convert XML file into array in PHP? Given an XML document and the task is to convert an XML file into PHP array. To convert the XML document into PHP array, some PHP functions are used which are listed below: file_get_contents() function: The file_get_contents() function is used to read a file as string. This function uses memory mapp
2 min read
How to Flatten Multidimentional Array in PHP ? Given a Multi-dimensional array, the task is to flatten the multidimensional array into a simple array (1-dimensional array) in PHP.Examples:Input: arr = [[1, 2], [3, 4, 5], 6]Output: [1, 2, 3, 4, 5, 6]Input: arr = [[1, 4, 5], [6, 7, 8]]Output: [1, 4, 5, 6, 7, 8]Working with multidimensional arrays
3 min read
How to check whether an array is empty using PHP? Arrays are commonly used data structures that can hold multiple values. Sometimes it is necessary to check if an array is empty or not, and it is an important task to avoid errors when working with data. PHP offers several easy and effective ways to check if an array is empty. Below are the most com
3 min read
Program to remove empty array elements in PHP Given an array containing elements. The task is to remove empty elements from the array such as an empty string or a NULL element. Method 1: Using array_filter() Function. It is achieved by using array_filter() function. It also removes false values when declared using a callback function, however,
2 min read
Get the First & Last Item from an Array in PHP We will get the first and last elements of an array in PHP. This can be particularly useful in scenarios like processing data, working with queues, or implementing specific algorithms. Below are the approaches to get the first and last items from an array in PHP: Table of Content Using Array Indexin
2 min read