如果将一个关联数组添加到另一个关联数组的前面,则添加到前面的数组将被按数字索引到前一个数组中。
<?php
$foods = [
'apples' => [
'McIntosh' => 'red',
'Granny Smith' => 'green',
],
'oranges' => [
'Navel' => 'orange',
'Valencia' => 'orange',
],
];
$vegetables = [
'lettuce' => [
'Iceberg' => 'green',
'Butterhead' => 'green',
],
'carrots' => [
'Deep Purple Hybrid' => 'purple',
'Imperator' => 'orange',
],
'cucumber' => [
'Kirby' => 'green',
'Gherkin' => 'green',
],
];
array_unshift($foods, $vegetables);
var_dump($foods);
array(3) {
[0] =>
array(3) {
'lettuce' =>
array(2) {
'Iceberg' =>
string(5) "green"
'Butterhead' =>
string(5) "green"
}
'carrots' =>
array(2) {
'Deep Purple Hybrid' =>
string(6) "purple"
'Imperator' =>
string(6) "orange"
}
'cucumber' =>
array(2) {
'Kirby' =>
string(5) "green"
'Gherkin' =>
string(5) "green"
}
}
'apples' =>
array(2) {
'McIntosh' =>
string(3) "red"
'Granny Smith' =>
string(5) "green"
}
'oranges' =>
array(2) {
'Navel' =>
string(6) "orange"
'Valencia' =>
string(6) "orange"
}
}