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.]
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.
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.
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.
Use list()
:
list($a, $b, $c) = $someArray;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With