PHP 中有元组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22083760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:39:21  来源:igfitidea点击:

Are there tuples in PHP?

phptuples

提问by brian

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

我知道在 Python 和其他语言中,我们可以访问元组以在语义或其他方面更好地促进数据的结构化。

My question is: Does PHP have tuples?

我的问题是:PHP 有元组吗?

If not, what is the nearest facility?

如果没有,最近的设施是什么?

采纳答案by Daniel Williams

PHP's only real built in data structure that people use for everything is the array.

PHP 唯一真正的内置数据结构是人们使用的所有数据结构是数组。

Arrays in PHP are all hash tables and can have either numeric or string indexes and can contain anything (usually more arrays).

PHP 中的数组都是哈希表,可以有数字或字符串索引,并且可以包含任何内容(通常是更多的数组)。

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/manual/en/language.types.array.php

http://us1.php.net/list

http://us1.php.net/list

List is very convenient for returning multiple values from a function.

List 对于从函数返回多个值非常方便。

回答by Damien Black

Arrays in PHP can be used very much like a tuple:

PHP 中的数组可以像元组一样使用:

// 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:

从 PHP 7.1 开始,您还可以像这样解压缩:

[$a, $b, $c] = $x;

回答by Blue Ice

From https://coderwall.com/p/bah4oq:

来自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.

您可以像元组一样传递要使用的对象数组。您还可以一次在数组中存储异构类型,这非常类似于元组的功能。

回答by zsolt

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:

前面的答案是正确的,因为您可以使用 PHP 数组模拟元组,但是仅在有限的意义上,并非所有用例。当您想使用元组作为映射的键时,这一点很明显。例如。在实现状态机时,转换表中的键将是元组:

(currState, Event) -> newState

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

如果我们在 PHP 中有适当的元组支持,我们可以做这样的事情:

$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.

两者都实现了相同的结果,但第一个具有明确的语义。

回答by Alek

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.

从 PHP 7.1 开始,引入了对称数组解构,它允许您以与list()之前评论中提到的相同的方式进行分配。

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

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