1

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.

2
  • 3
    Not being a C# person, can you describe in words what the above does and what you want to use it for? Commented Dec 13, 2012 at 17:02
  • $poules = array($aantal, $aantal, 2); or $poules = [$aantal, $aantal, 2]; in php 5.4. or Commented Dec 13, 2012 at 17:05

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's not really the same as a three-dimensional array, is it?
Yup, looks more multi-dimensional. It's also madness. ;-) Well, at least it's not very typical PHP code.
1

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.

Comments

0

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

1 Comment

I don't think this is the same thing.
0
$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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.