Core PHP

Unpacking Arrays

This is an example PHP program that demonstrates how to unpack arrays and the effect that it has.

UnpackingArrays.php

<?php

// Create a numerically-indexed array.
$saArray1 = ['zero','one','two','three','four'];

// Create a second numerically-indexed array.
$saArray2 = ['ten','twenty','thirty','forty'];

// Unpack those arrays into a third numerically-indexed array (requires PHP 7.4)
$saArray3 = ['Father', ...$saArray1, 'Son', ...$saArray2, 'Holy Spirit'];

// Display the resulting array
foreach($saArray3 as $iKey => $sValue) {
  echo "{$iKey} => {$sValue}<br />";
}
echo "<hr />";

// Create an associative array
$saAssoc1 = ['Major1' => 'Isaiah', 'Major2' => 'Daniel'];

// Create a second associative array
$saAssoc2 = ['Major3' => 'Jeremiah', 'Major4' => 'Ezekiel'];

// Unpack those into a third associative array (requires PHP 8.1) - values may be overwritten
$saAssoc3 = ['Minor1' => 'Amos', ...$saAssoc1, 'Minor2' => 'Jonah', ...$saAssoc2, 'Minor3' => 'Joel'];

// Display the resulting array
foreach($saAssoc3 as $iKey => $sValue) {
  echo "{$iKey} => {$sValue}<br />";
}

?>
 

Output

0 => Father
1 => zero
2 => one
3 => two
4 => three
5 => four
6 => Son
7 => ten
8 => twenty
9 => thirty
10 => forty
11 => Holy Spirit

Minor1 => Amos
Major1 => Isaiah
Major2 => Daniel
Minor2 => Jonah
Major3 => Jeremiah
Major4 => Ezekiel
Minor3 => Joel
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.