我有两个雄辩的输出
allowance Amount
A 100
B 80
C 120
D 150
AND
Deductions Amount
X 50
Y 60
Z 70
和json显示为
$allowance = [{"allname":"A","allamount":"100"},{"allname":"B","allamount":"80"},{"allname":"C","allamount":"120"},{"allname":"D","allamount":"150"}]
$Deductions = [{"dedname":"X","dedamount":"50"},{"dedname":"Y","dedamount":"60"},{"dedname":"Z","dedamount":"70"}].
我希望新的json像
$new = [{"allname":"A","allamount":"100","dedname":"X","dedamount":"50"},{"allname":"B","allamount":"80","dedname":"Y","dedamount":"60"},{"allname":"C","allamount":"120","dedname":"Z","dedamount":"70"},{"allname":"D","allamount":"150"}]
请引导我。预先感谢。
答案 0 :(得分:1)
假设您有两个收藏夹
$firstCollection = collect(['one','two']);
$collectionTwo = collect(['three','four']);
$firstCollection->merge($collectionTwo);
使用merge()
将合并两个集合。
答案 1 :(得分:0)
尝试以下代码即可获得所需的内容:
$a = array(array('allname' =>'A','allamount'=>'100'),
array('allname' =>'B','allamount'=>'200'),
array('allname' =>'C','allamount'=>'300'));
$b = array(array('dedname' =>'X','dedamount'=>'50'),
array('dedname' =>'c','dedamount'=>'150'));
$all=array();
for ($i=0; $i < count($a); $i++) {
$merged="";
if (isset($b[$i])) {
$merged=array_merge($a[$i],$b[$i]);
}else{
$merged=$a[$i];
}
$all[]=$merged;
}
echo "<pre>";print_r($all);echo "<br";
header('Content-Type: application/json');
echo json_encode($all);
输出:
[{“ allname”:“ A”,“ allamount”:“ 100”,“ dedname”:“ X”,“ dedamount”:“ 50”},{“ allname”:“ B”,“ allamount” :“ 200”,“ dedname”:“ c”,“ dedamount”:“ 150”},{“ allname”:“ C”,“ allamount”:“ 300”}]
希望这对您有所帮助。