How do i create an empty array in php?

$myArray = []; 

Creates empty array.

You can push values onto the array later, like so:

$myArray[] = "tree";
$myArray[] = "house";
$myArray[] = "dog";

At this point, $myArray contains "tree", "house" and "dog". Each of the above commands appends to the array, preserving the items that were already there.

Having come from other languages, this way of appending to an array seemed strange to me. I expected to have to do something like $myArray += "dog" or something... or maybe an "add()" method like Visual Basic collections have. But this direct append syntax certainly is short and convenient.

You actually have to use the unset() function to remove items:

unset($myArray[1]); 

... would remove "house" from the array (arrays are zero-based).

unset($myArray); 

... would destroy the entire array.

To be clear, the empty square brackets syntax for appending to an array is simply a way of telling PHP to assign the indexes to each value automatically, rather than YOU assigning the indexes. Under the covers, PHP is actually doing this:

$myArray[0] = "tree";
$myArray[1] = "house";
$myArray[2] = "dog";

You can assign indexes yourself if you want, and you can use any numbers you want. You can also assign index numbers to some items and not others. If you do that, PHP will fill in the missing index numbers, incrementing from the largest index number assigned as it goes.

So if you do this:

$myArray[10] = "tree";
$myArray[20] = "house";
$myArray[] = "dog";

... the item "dog" will be given an index number of 21. PHP does not do intelligent pattern matching for incremental index assignment, so it won't know that you might have wanted it to assign an index of 30 to "dog". You can use other functions to specify the increment pattern for an array. I won't go into that here, but its all in the PHP docs.

Cheers,

-=Cameron

How do i create an empty array in php?
NDUKWE CHIDERA K.

Overview

An array in PHP is used to hold multiple values that may or may not be of the same data type. In PHP, an array is of three basic types:

  • Indexed array
  • Associative array
  • Multidimensional array

In PHP, arrays are initialized with the array keyword, as shown below:

$myArray = array();

How to initialize empty arrays

You may ask, “Why do I need to initialize an array without a value?” This is common in PHP codes, where you may want to store the values of a future computation into an array. You basically would declare the array as empty and store the computed values into it.

So, how can this be achieved?

PHP arrays with no value can be initialized in three ways:

  1. $myArray = array();

  2. $myArray = [];

  3. $myArray = (array) null;

Among the three methods shown, the most common among programmers is the second, which uses []. This is because the second method is marginally faster as it is only a constructor and is just simpler to use.

Code

Let’s look at some code that uses the techniques listed above.

<?php

$empty1 = (array) null;

$empty2 = [];

$empty3 = array();

var_dump($empty1,$empty2,$empty3);

//All above array are empty and will return empty

//pushing values into second type of array

$empty[] ="trails" ;

$empty[] ="trips" ;

$empty[] ="tricks" ;

$empty[] ="trails" ;

var_dump ($empty) ;

?>

CONTRIBUTOR

How do i create an empty array in php?
NDUKWE CHIDERA K.

On this page, you have an opportunity to get a step-by-step guide, teaching how to add elements to an empty array in PHP.

The first step should be creating an empty array.

You can generate an empty PHP array with the help of the array() function like this:

$emptyArray = []; 
$emptyArray = array();
$emptyArray = (array) null;

It is essential to create an empty array and then push elements to it. The reason is that it assists in preventing errors linked to a faulty array. Also, it helps to save time in the process of debugging.

Please, take into account that PHP 5.4 supports [] as an alternative means. Hence, now most of the developers choose to work with x $array = []. The primary reason is that it allows going back and forth between PHP and JavaScript easier.

Here is an example of creating an empty array with x$array = []:

<?php
  
/* method of creating an Empty array. */
$firstempty = [];
echo "Created First empty array <br>";
      
/* method of creating the Second Empty array. */
$second = array( );
echo "Created second empty array<br>";
      
/* the First method of creating an array. */
$first = array( 1, 2);
          
foreach( $first as $value ) {
    echo "Value is $value <br>";
}
          
/* The Second method of creating an array. */
$first[0] = "one";
$first[1] = "two";
          
foreach( $first as $value ) {
    echo "Value is $value <br>";
}
?>

And, here is another shorter method:

<?php 
  
// Create an empty array 
$emptyArray=array(); 
  
// Push elements to the array 
array_push($emptyArray, "geeks", "for", "geeks"); 
  
// Display array elements 
print_r($emptyArray); 
?>

After creating an empty array, you can get to adding elements to it. Below, you can find several ways to add elements to an array, depending on your exact needs.

Using array_unshift

If you intend to add elements to the beginning of the array, you are recommended to use the array_unshift() function like this:

$ar = array('Lili', 'Kiki', 'Lolo', 'Coco');
// use array_unshift to add 2 elements to beginning of $ar
$num = array_unshift($ar, 'Lucy', 'Marlo');
// inspect return value of array_unshift
echo $num; // 6
print_r($ar);
/* Array
(
    [0] => Lucy
    [1] => Marlo
    [2] => Lili
    [3] => Kiki
    [4] => Lolo
    [5] => Coco
) */

Using array_push

If you intend to add elements to the end of your array, then you can use the xarray_push() function.

Here is an example:

<?php
$a=array("white","blue");
array_push($a,"green","pink");
print_r($a);
?>

This function will add green and pink colours to your array.

Using $cart [ ]

The next way to add elements to an empty array is using the $cart[]= syntax.

Here is an example:

$cart = array(); 
$cart[] = 13; 
$cart[] = 14;
for($i=0;$i<=5;$i++) { 
   $cart[] = $i; 
} 
echo "<pre>"; print_r($cart); echo "</pre>";

So, in this snippet we represented to you the steps to create an empty array and add elements to it. After reading and checking out the examples, you can choose the option that is more convenient for your project.

How do you create an empty array?

1) Assigning it to a new empty array This is the fastest way to empty an array: a = []; This code assigned the array a to a new empty array. It works perfectly if you do not have any references to the original array.

Is empty array in PHP?

php $emptyArray = array(); $isEmpty = empty($emptyArray); echo("The function has returned $isEmpty. ... Use empty() Function to Check Whether an Array Is Empty in PHP..

How do you declare and initialize an array in PHP?

PHP array() Function.
Create an indexed array named $cars, assign three elements to it, and then print a text containing the array values: ... .
Create an associative array named $age: ... .
Loop through and print all the values of an indexed array: ... .
Loop through and print all the values of an associative array:.

Is an empty array empty PHP?

An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).