我想从PHP中的JSON表创建另一个JSON对象。这是我的JSON对象。我想将所有相同的帐户分组到一个数组中。
$ entry_titles是
[
{
"account": "A",
"month": 2016-01,
"amount": 5000
},
{
"account": "A",
"month": 2016-02,
"amount": 10000
},
{
"account": "B",
"month": 2016-01,
"amount": 7000
},
{
"account": "B",
"month": 2016-02,
"amount": 14000
}
]
并希望将其转换为
[{
"account": "A",
"2016-01": 5000,
"2016-02": 10000
}, {
"account": "B",
"2016-01": 7000,
"2016-02": 14000
}]
如何通过account
属性对数组中的所有对象进行分组?
我试过这段代码......
$entry_titles = json_decode($entry, true);
$store_account_id='';
$json_new=array();
foreach ($entry_titles as $row)
{
$account_id=$row['account_id'];
$amount=$row['amount'];
$month=$row['month'];
if ($store_account_id!=$account_id)
{
$json_new['account_id']=$account_id;
$json_new[$month]=$amount;
}
else
{
$json_new[$month]=$amount;
$data_new[]=$json_new;
}
$store_account_id=$account_id;
};