Reputation: 21
In C# you can do this:
poules = new int[aantal, aantal, 2];
I couldn't find a way in PHP to do this. All I can find is a two dimensional array.
Upvotes: 1
Views: 447
Reputation: 2768
The example you presented is creating a 3D array, and getting it by a method (which is a bit slower, since a method is used). PHP uses jagged arrays.
An array in PHP is created using the array
function.
To match your example: (for fixed array length)
$myArr = new SplFixedArray($aantal);
for ($i = 0; $i < $aantal; $i++) {
$myArr[$i] = new SplFixedArray($aantal);
for ($j = 0; $j < $aantal; $j++) {
$myArr[$i][$j] = new SplFixedArray(2);
}
}
SplFixedArray
is used to define an array with a fixed size.
As claimed in comments, you will rarely see a PHP code as the above.
you can access the array cells like this:
$val = $myArr[$x][$y][$z];
Here is a reference: SplFixedArray
Upvotes: 3
Reputation: 522015
I'm not a C# person, so take this with a grain of salt. After perusing the documentation though, new int[aantal, aantal, 2]
seem to be the syntax to declare multi-dimensional int
arrays, in this case a 3-dimensional array.
PHP doesn't have multi-dimensional arrays. It only has arrays, and you can have arrays of arrays. I guess this is called a "jagged array" in C#. PHP arrays are also not typed, there's no equivalent to int[]
.
You can simply declare/access several dimensions at once though:
$arr = array();
$arr[1][2][3] = 'foo';
PHP will create the intermediate arrays as needed. The array is jagged though, there's no $arr[0][2][3]
after the above code executed.
If you need to pre-allocate all dimensions beforehand, you will have to recursively loop. This is not really something done very often in PHP though, you should adapt your practices to work with jagged arrays to get things done.
Upvotes: 1
Reputation: 12843
$poules = array('aantal', 'aantal', 2);
Or are aanta1 arrays too? Because in php you can do:
$myArray = array("some data", array("more data", "in a other array"), array("key1" => "even with keys"));
Upvotes: 0
Reputation: 41595
This would be a php array:
$poules = array('aantal', 'anntal', '2');
In case aantal
is a variable, just the same without quotes:
$poules = array($aantal, $anntal, '2');
In PHP you don't need to specify the variable type. Although you can use it to validate or filter inputs.
You can read more about PHP arrays in the official documentation:
https://www.php.net/manual/en/language.types.array.php
Upvotes: 0