我有两个数组,一个看起来像这样:
$运费
Array ( [0] => Array (
[code] => sub_total
[title] => Sub-Total
[text] => $1,728.00
[value] => 1728
[sort_order] => 1 )
另一个是这样的:
$totals
[0] => Array (
[code] => tax
[title] => Georgia Sales Tax
[text] => $120.96
[value] => 120.96
[sort_order] => 5 )
[1] => Array (
[code] => total
[title] => Total
[text] => $1,848.96
[value] => 1848.96
[sort_order] => 9 )
[2] => Array (
[code] => free.free
[title] => Free Shipping
[cost] => 0
[tax_class_id] => 0
[text] => $0.00 ) )
我想将数组合并在一起,并使用foreach
显示它们的title
和text
值。然而,我不希望数组$shipping
在开头或结尾,而是看起来像这样:
Sub-Total $1,728.00
Free Shipping $0.00
Georgia Sales Tax $120.96
Total $1,848.96
无论如何,这是可以做到的?
编辑:
我希望我的阵列按以下顺序显示:
[0] => Array (
[code] => tax
[title] => Georgia Sales Tax
[text] => $120.96
[value] => 120.96
[sort_order] => 5 )
[1] => Array ( // <--- this is where I want the $shipping array
[code] => free.free
[title] => Free Shipping
[cost] => 0
[tax_class_id] => 0
[text] => $0.00 ) )
[2] => Array (
[code] => sub_total
[title] => Sub-Total
[text] => $1,728.00
[value] => 1728
[sort_order] => 1 )
[3] => Array (
[code] => total
[title] => Total
[text] => $1,848.96
[value] => 1848.96
[sort_order] => 9 )
这就是您想要的。
array_splice($totals, 2, 0, $shipping);
这将把装运数组插入到第三个位置的totals数组中,就像您的解决方案示例中一样。
使用array_merge()
$newArray = array_merge($totals, $shipping);
假设$shipping总是有一个项目
$newArray = array_push($totals, $shipping[0])