我有一个使用cakephp模型检索的数组。这是我检索的样本格式。
{
[0]=> {
["User"]=> {
["id"]=> "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
["name"]=> "Alex"
["username"]=> "J111111M" }
["FmRptGaTransaction"] => {
["id"]=> "1542667"
["organisation_id"]=> "20544"
["txn_date"]=> "2017-12-01"
["txn_type"]=> "school_fee"
["txn_desc"]=> "Program Fee (December 2017)"
}
}
[1]=> {
["User"]=> {
["id"]=> "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
["name"]=> "Alex"
["username"]=> "J111111M" }
["FmRptGaTransaction"] => {
["id"]=> "1542731"
["organisation_id"]=> "20544"
["txn_date"]=> "2017-11-01"
["txn_type"]=> "school_fee"
["txn_desc"]=> "Program Fee (November 2017)"
}
}
[2]=> {
["User"]=> {
["id"]=> "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
["name"]=> "Alex"
["username"]=> "J111111M" }
["FmRptGaTransaction"] => {
["id"]=> "1542734"
["organisation_id"]=> "20544"
["txn_date"]=> "2017-10-01"
["txn_type"]=> "school_fee"
["txn_desc"]=> "Program Fee (September 2017)"
}
}
}
我想要的是这样的东西;
{
[0]=> {
["User"]=> {
["id"]=> "57ba6fcb-c214-410d-98dc-5b3c80c7bd6a"
["name"]=> "Alex"
["username"]=> "J111111M"
}
["FmRptGaTransaction"] => {
[0]=>{
["id"]=> "1542667"
["organisation_id"]=> "20544"
["txn_date"]=> "2017-12-01"
["txn_type"]=> "school_fee"
["txn_desc"]=> "Program Fee (December 2017)"
}[1]=>{
["id"]=> "1542731"
["organisation_id"]=> "20544"
["txn_date"]=> "2017-11-01"
["txn_type"]=> "school_fee"
["txn_desc"]=> "Program Fee (November 2017)"
}[2]=>{
["id"]=> "1542734"
["organisation_id"]=> "20544"
["txn_date"]=> "2017-10-01"
["txn_type"]=> "school_fee"
["txn_desc"]=> "Program Fee (September 2017)"
}
}
}
}
关键是如果用户已经存在,我想要显示一次。然后,事务数组的其余部分想要创建另一个数组。
我做的是......
$gaData = [];
$extUserId = [];
foreach($rptGaData as $data) {
if(!in_array($data['User']['id'],$extUserId)){ //Want to create one time if User array is already exist
$temp = [];
$temp['User']['id'] = $data['User']['id'];
$temp['User']['name'] = $data['User']['name'];
$temp['User']['username'] = $data['User']['username'];
}
$transaction = [];
$transaction['id'] = $data['FmRptGaTransaction']['id'];
$transaction['organisation_id'] = $data['FmRptGaTransaction']['organisation_id'];
$transaction['txn_date'] = $data['FmRptGaTransaction']['txn_date'];
$transaction['txn_type'] = $data['FmRptGaTransaction']['txn_type'];
$transaction['txn_desc'] = $data['FmRptGaTransaction']['txn_desc'];
$temp['User']['FmRptGaTransaction'][] = $transaction;
$gaData[] = $temp;
$extUserId[] = $data['User']['id'];
}
但是,听起来我的最终结果有些不对劲。
答案 0 :(得分:6)
尝试此操作(使用用户ID作为数组键):
$gaData = [];
foreach($rptGaData as $data) {
$temp = [];
if(!isset($gaData[$data['User']['id']])){ //Want to create one time if User array is already exist
$temp['User']['id'] = $data['User']['id'];
$temp['User']['name'] = $data['User']['name'];
$temp['User']['username'] = $data['User']['username'];
}
$transaction = [];
$transaction['id'] = $data['FmRptGaTransaction']['id'];
$transaction['organisation_id'] = $data['FmRptGaTransaction']['organisation_id'];
$transaction['txn_date'] = $data['FmRptGaTransaction']['txn_date'];
$transaction['txn_type'] = $data['FmRptGaTransaction']['txn_type'];
$transaction['txn_desc'] = $data['FmRptGaTransaction']['txn_desc'];
$temp['FmRptGaTransaction'][] = $transaction;
if(isset($gaData[$data['User']['id']]))
$gaData[$data['User']['id']]['FmRptGaTransaction'][] = $transaction;
else
$gaData[$data['User']['id']]=$temp;
}
echo "<pre>";
print_r($gaData);