How to create an empty array in PHP with predefined size?
There is no way to create an array of a predefined size without also supplying values for the elements of that array.
The best way to initialize an array like that is array_fill
. By far preferable over the various loop-and-insert solutions.
$my_array = array_fill(0, $size_of_the_array, $some_data);
Every position in the $my_array
will contain $some_data
.
The first zero in array_fill
just indicates the index from where the array needs to be filled with the value.
In php how do I set the size of an array?
Use SplFixedArray for a fixed-size array:
$array = new SplFixedArray(3);
$array[0] = 1;
$array[1] = 2;
$array[2] = 3;
$array[3] = 4; // RuntimeException
Is there a PHP equivalent of new Array (number) in javascript?
Actually, in this context, you're creating an array of size 13
.
You don't really need to preallocate arrays in PHP, but you can do something like:
$result = array_fill( 0, 12, null);
This will create an array with 13 elements (indexes 0 through 12) whose values are null
.
Best way to initialize (empty) array in PHP
In ECMAScript implementations (for instance, ActionScript or JavaScript), Array()
is a constructor function and []
is part of the array literal grammar. Both are optimized and executed in completely different ways, with the literal grammar not being dogged by the overhead of calling a function.
PHP, on the other hand, has language constructs that may look like functions but aren't treated as such. Even with PHP 5.4, which supports []
as an alternative, there is no difference in overhead because, as far as the compiler/parser is concerned, they are completely synonymous.
// Before 5.4, you could only write
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// As of PHP 5.4, the following is synonymous with the above
$array = [
"foo" => "bar",
"bar" => "foo",
];
If you need to support older versions of PHP, use the former syntax. There's also an argument for readability but, being a long-time JS developer, the latter seems rather natural to me. I actually made the mistake of trying to initialise arrays using []
when I was first learning PHP.
This change to the language was originally proposed and rejected due to a majority vote against by core developers with the following reason:
This patch will not be accepted because slight majority of the core developers voted against. Though if you take a accumulated mean between core developers and userland votes seems to show the opposite it would be irresponsible to submit a patch witch is not supported or maintained in the long run.
However, it appears there was a change of heart leading up to 5.4, perhaps influenced by the implementations of support for popular databases like MongoDB (which use ECMAScript syntax).
Quickly create large arrays in PHP
In PHP, you don't have to predefine the size of your array, you can dynamically append it with data, like so:
$bar = array();
for($i = 0;$i < 50;$i++) {
$bar[] = $i;
}
By calling the $bar with [], you'll indicate that a new key should be created and in this case hold value $i.
How to initialize an empty array of object with a predefined array?
Use a map:
var newArray = admins.map((admin) => ({ name: admin }));
Related Topics
Unknown Modifier '/' in ...? What Is It
Why Is PHP Not Replacing the Variable in String
Can Servers Block Curl Requests
Fatal Error: Out of Memory, But I Do Have Plenty of Memory (Php)
How to Convert These Strange Characters? (ë, Ã, ì, ù, Ã)
Difference Between Var_Dump,Var_Export & Print_R
PHP - Extracting a Property from an Array of Objects
How to Convert Pascalcase to Snake_Case
Whats the Easiest Way to Determine If a User Is Online? (Php/Mysql)
How to Return a Proper Success/Error Message for Jquery .Ajax() Using PHP
How to Remove the Leading Character from a String
Calculate Total Seconds in PHP Dateinterval
Phpmyadmin Error: the Mbstring Extension Is Missing. Please Check Your PHP Configuration
How to Check If a Directory Exists? "Is_Dir", "File_Exists" or Both
"No Such File or Directory" or "No Such Host Is Known" When Running Migrations
PHP Crop Image to Fix Width and Height Without Losing Dimension Ratio