Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there tuples in PHP?

Tags:

php

tuples

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?

like image 999
brian Avatar asked Feb 28 '14 00:02

brian


People also ask

Are there lists in PHP?

The list function in PHP is an inbuilt function which is used to assign the array values to multiple variables at a time while execution. Like array (), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.

Are tuples the same as arrays?

Tuples have a slight performance improvement to lists and can be used as indices to dictionaries. Arrays only store values of similar data types and are better at processing many values quickly.

Can tuples have arrays?

Tuples are a sort of list but with a limited set of items. In JavaScript, tuples are created using arrays. In Flow you can create tuples using the [type, type, type] syntax. When you are getting a value from a tuple at a specific index, it will return the type at that index.

Is a tuple an object?

A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.


2 Answers

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.

like image 31
Daniel Williams Avatar answered Sep 19 '22 19:09

Daniel Williams


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; 
like image 154
Damien Black Avatar answered Sep 21 '22 19:09

Damien Black