Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel array assignment in PHP

Tags:

Most languages make it easy to take an array like [1, 2, 3] and assign those values to variables a, b, and c with a single command. For example, in Perl you can do

($a, $b, $c) = (1, 2, 3); 

What's the corresponding trick in PHP?

[Thanks so much for the lightning fast answer! I know this is a trivial question but all the obvious google queries didn't turn up the answer so this is my attempt to fix that.]

like image 246
dreeves Avatar asked Feb 02 '10 07:02

dreeves


People also ask

What is parallel array explain with an example?

Parallel arrays use two or more arrays to represent a collection of data where each corresponding array index is a matching field for a given record. For example, if there are two arrays, one for names and one for ages, the array elements at names[2] and ages[2] would describe the name and age of the third person.

What is parallel array in data structure?

In computing, a group of parallel arrays (also known as structure of arrays or SoA) is a form of implicit data structure that uses multiple arrays to represent a singular array of records. It keeps a separate, homogeneous data array for each field of the record, each having the same number of elements.

When can parallel arrays be used?

Parallel arrays come in handy when data regarding different characteristics of the same subject of interest needs to be stored and accessed efficiently. The diagram below shows two parallel arrays. The first array stores product names, and the second array stores the price of each product.


1 Answers

Use list():

list($a, $b, $c) = $someArray; 
like image 60
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 20:09

Ignacio Vazquez-Abrams