根据对象键和值对对象进行分组
Group the objects based on object key and value
我的礼物
{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Public",
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public"
}
}
我想要这样的分组对象
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
}
},
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-0",
"date": "January 05, 2021",
"type": "Private"
},
}
}
假设您希望它们按日期分组,然后再按帐户类型分组,您可以这样做。
考虑到您已将 json 转换为数组数组,一个相对简单的 foreach 循环应该可以解决问题。
$filteredAccounts = [];
foreach ($accounts as $account) {
$date = new \DateTime($account['date']);
$filteredAccounts[$date->format('Y-m-d')][$account['type']][] = $account;
}
// Then you build your final array you want.
$groupedAccounts = [];
foreach ($filteredAccounts as $date => $filteredAccount) {
$filteredAccount['Date'] = $date;
$groupedAccounts[] = $filteredAccount;
}
var_dump($groupedAccounts);
但正如其他人提到的那样,您的问题与 Laravel 没有任何关系,但无论如何,如果您想要以原始方式解决问题,这应该对您有所帮助。
很遗憾,您提供的输入数据无效 JSON。缺少逗号或在错误的位置,外部大括号应该是方括号。这是一个“更正”版本和一种“尽可能接近”地生成您想要的输出的方法:
$in='[
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-04 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Public"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Public"
}
]';
$out=array_reduce(json_decode($in), function($a,$c){
$a[substr($c->cd,0,10)][$c->type][]=[
"_id"=>$c->_id,
"user_id"=>$c->user_id,
"account_status"=>$c->account_status,
"compare_date"=>$c->compare_date,
"date"=>$c->date,
"type"=>$c->type ];
return $a;}, [] );
array_walk($out,function($arr,$key) use(&$res) {
$arr['Date']=$key;
array_walk($arr, function($ar,$k) use($key,&$res) {
$res[$key][$k]=$ar;
});
});
foreach ($res as $el) $result[]=$el;
print_r($result);
您可以在此处找到工作演示:https://rextester.com/BQWU57249
这是输出:
array
(
[0] => Array
(
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-04
)
[1] => Array
(
[Private] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Private
)
[1] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Private
)
)
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-05
)
)
我的礼物
{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Public",
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public"
}
}
我想要这样的分组对象
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
}
},
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-0",
"date": "January 05, 2021",
"type": "Private"
},
}
}
假设您希望它们按日期分组,然后再按帐户类型分组,您可以这样做。
考虑到您已将 json 转换为数组数组,一个相对简单的 foreach 循环应该可以解决问题。
$filteredAccounts = [];
foreach ($accounts as $account) {
$date = new \DateTime($account['date']);
$filteredAccounts[$date->format('Y-m-d')][$account['type']][] = $account;
}
// Then you build your final array you want.
$groupedAccounts = [];
foreach ($filteredAccounts as $date => $filteredAccount) {
$filteredAccount['Date'] = $date;
$groupedAccounts[] = $filteredAccount;
}
var_dump($groupedAccounts);
但正如其他人提到的那样,您的问题与 Laravel 没有任何关系,但无论如何,如果您想要以原始方式解决问题,这应该对您有所帮助。
很遗憾,您提供的输入数据无效 JSON。缺少逗号或在错误的位置,外部大括号应该是方括号。这是一个“更正”版本和一种“尽可能接近”地生成您想要的输出的方法:
$in='[
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-04 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Public"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Public"
}
]';
$out=array_reduce(json_decode($in), function($a,$c){
$a[substr($c->cd,0,10)][$c->type][]=[
"_id"=>$c->_id,
"user_id"=>$c->user_id,
"account_status"=>$c->account_status,
"compare_date"=>$c->compare_date,
"date"=>$c->date,
"type"=>$c->type ];
return $a;}, [] );
array_walk($out,function($arr,$key) use(&$res) {
$arr['Date']=$key;
array_walk($arr, function($ar,$k) use($key,&$res) {
$res[$key][$k]=$ar;
});
});
foreach ($res as $el) $result[]=$el;
print_r($result);
您可以在此处找到工作演示:https://rextester.com/BQWU57249
这是输出:
array
(
[0] => Array
(
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-04
)
[1] => Array
(
[Private] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Private
)
[1] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Private
)
)
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-05
)
)