11

I was trying to convert basic Javascript function into PHP, and I saw that one of the variables was declared var Variable = new Array (13).

I know that PHP variables are declared like: $variable = array()

but what about the "13" in new Array(13)? does that translate to $variable = array(13)? I've tried that but it didn't seem to work.

This in Javascript

var results = new Array (13);

becomes this in PHP, am I correct?

$results = array(13);
nickb
  • 59,313
  • 13
  • 108
  • 143
ParaChase
  • 319
  • 6
  • 15
  • 1
    might be a related question: http://stackoverflow.com/questions/5385433/how-to-create-an-empty-array-in-php-with-predefined-size – Wern Ancheta Jul 09 '12 at 23:04

5 Answers5

16

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.

nickb
  • 59,313
  • 13
  • 108
  • 143
7

Also note there is SplFixedArray for fixed sized arrays

$array = new SplFixedArray(5);

http://php.net/manual/en/class.splfixedarray.php

Petah
  • 45,477
  • 28
  • 157
  • 213
5

new Array(someNumber) in JS creates an array containing someNumber elements which are undefined. Unless you want that you should always use [...] to create an array with some elements since that syntax is not ambiguous while new Array() works differently depending on the number and type of arguments.

array(13) in PHP however creates an array containing a single value 13.

A roughly similar function in PHP is array_fill(0, 12, null). However, PHP might allocate an empty array and then grow it while filling it.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

No, putting a number inside the parenthesis in PHP adds one value to the array that is the int 13. Whereas a number in JS inside the parenthesis makes an array with that number of undefined values.

Arrays in PHP are dynamic, you don't need to declare their size like you sometimes do in JS.

John V.
  • 4,652
  • 4
  • 26
  • 26
  • You don't need to declare the size in JavaScript, unless you have a good reason to use an array that has exactly `n` instances of `undefined`. – zzzzBov Jul 09 '12 at 22:59
  • @zzzzBov Thanks, I'm not as familiar with JS as PHP. Slightly edited answer. – John V. Jul 09 '12 at 23:00
1

This creates an array with 13 undefined values:

var results = new Array (13);

This creates an array with the first index (index 0) equal to 13:

$results = array(13);

So that's incorrect. If you want the same result as that of javascript you'd like something like this:

$results = array_fill(0, 13, '');

You might also want to check out this question:

How to create an empty array in PHP with predefined size?

Community
  • 1
  • 1
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • Only working solution there ! considering others answered with array_fill(0, 12), thus leading to a 12 column table instead of 13... – jav974 Mar 08 '19 at 10:47