Are there tuples in PHP?

PhpTuples

Php Problem Overview


I know in Python and other languages we have access to tuples to better facilitate, semantically or otherwise, the structuring of data.

My question is: Does PHP have tuples?

If not, what is the nearest facility?

Php Solutions


Solution 1 - Php

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

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 - Php

As of PHP 7.1 Symmetric Array Destructuring was introduced which allows you to assign in the same way as list() as mentioned in previous comments.

// list() style
list($id1, $name1) = [1, 'Hello']; // Same as $id1 = 1 and $name1 = 'Hello'

// [] style
[$id2, $name2] = [2, 'World']; // Same as $id2 = 2 and $name2 = 'World'

http://php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring

Solution 4 - Php

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.

Solution 5 - Php

The previous answers are correct in that you can simulate tuples with PHP arrays, however only in a limited sense, not all use cases. This is evident when you want to use tuples as keys to a map. Eg. when implementing a state machine, keys in your transition table would be tuples:

(currState, Event) -> newState

If we had proper tuple support in PHP we could do something like this:

$trTable = [
    ['currState', 'fooEvent'] => 'fooState',
    ['currState', 'barEvent'] => 'barState',
    ...
];

Instead we have to simulate it like this:

$trTable = [
    'currState' => [
        'fooEvent' => 'fooState',
        'barEvent' => 'barState',
    ],
    ...
];

Both achieve the same result, but the first one has clear semantics.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrianView Question on Stackoverflow
Solution 1 - PhpDamien BlackView Answer on Stackoverflow
Solution 2 - PhpDaniel WilliamsView Answer on Stackoverflow
Solution 3 - PhpAlekView Answer on Stackoverflow
Solution 4 - PhpBlue IceView Answer on Stackoverflow
Solution 5 - PhpzsoltView Answer on Stackoverflow