一段时间以来,我一直对此表示怀疑,但该网站上没有其他回答的问题能够提供答案。我可能只是在搜索错误的字词,但我离题了。
我有两个来自HTML表单的数据数组。
Array1(付款说明)如下:
["1st Desc","2nd Desc","3rd Desc"]
和Array2(付款金额)类似:
["100.10","10.50","39.99"]
是否有一种方法可以组合这些数组(付款),使最终结果看起来像这样?
[{
"description": "1st Desc",
"amount": "100.10"
}, {
"description": "2nd Desc",
"amount": "10.50"
}, {
"description": "3rd Desc",
"amount": "39.99"
}]
答案 0 :(得分:4)
您似乎是SO的老会员,但您仍然没有遵循有关如何在此处寻求帮助的规则? 我敦促您以后再做 :)请参见here。顺便说一句,您可以通过简单的array_combine()
和foreach()
循环来实现。让我们尝试-
<?php
$arr1 = ["1st Desc","2nd Desc","3rd Desc"];
$arr2 =["100.10","10.50","39.99"];
$combined = array_combine($arr1,$arr2);
$expected = [];
foreach($combined as $k=>$v){
$expected[] = ['description'=>$k,'amount'=>$v];
}
echo json_encode($expected,JSON_PRETTY_PRINT);
?>
无 array_combine()
:
foreach($arr1 as $k=>$v){
$expected[$k] = ['description'=>$v,'amount'=>$arr2[$k]];
}