Are there tuples in PHP?
Solution 1:
Arrays in PHP can be used very much like a tuple:
// one dimensional mixed data
$x = [1, 2, "hello"];
// multidimensional third element
$y = [1, 2, [3, 4, 5]];
// assigning to variables (list unpacking)
list($a, $b, $c) = $x; //$a is 1, $b is 2, $c is "hello"
As of PHP 7.1 you can also unpack like this:
[$a, $b, $c] = $x;
Solution 2:
PHP's only real built in data structure that people use for everything is the array.
Arrays in PHP are all hash tables and can have either numeric or string indexes and can contain anything (usually more arrays).
There are a few array constructs that work like tuples.
See
http://us1.php.net/manual/en/language.types.array.php
http://us1.php.net/list
List is very convenient for returning multiple values from a function.
Solution 3:
From https://coderwall.com/p/bah4oq:
function foo()
{
return array('foo', 'bar');
}
list($a, $b) = foo();
You can pass an array of objects to be used just like a tuple. You can also store heterogeneous types in the array at once, which mimics very closely the functionality of a tuple.