【问题标题】:Group the objects based on object key and value [closed]根据对象键和值对对象进行分组[关闭]
【发布时间】:2021-04-12 05:54:19
【问题描述】:

我现在的物品

{
   {
     "_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"

        },


}


}

【问题讨论】:

  • 你能告诉我们你尝试了什么吗?本网站不是代码编写服务,您至少应该向我们展示您的尝试。
  • 你使用的是 laravel 还是 codeigniter,我想这两者都不是

标签: php arrays laravel codeigniter multidimensional-array


【解决方案1】:

很遗憾,您提供的输入数据不是有效的 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
        )
)

【讨论】:

  • 非常感谢@cars10m。它的工作真棒。
【解决方案2】:

假设您希望它们按日期分组,然后按帐户类型进一步分组,您可以这样做。

考虑到您已将 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 没有任何关系,但无论如何,如果您想要以原始方式获得它,这应该会帮助您。

【讨论】:

  • 谢谢你 Mikkel Schov。我会检查并更新你。
猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 2019-03-23
  • 2017-09-29
  • 2023-02-05
  • 2013-11-25
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多