Php 数组合并取决于数组键

Php 数组合并取决于数组键,php,arrays,Php,Arrays,如何合并具有相同键(使用日期作为键)但值不同的数组?我曾经创建过一个代码,但问题是每个键只存储一个值,但它们具有相同的键(日期) 这是我的阵列: { posting_date: "2017-08-08 00:00:00", id: 1, title: "activity 1", category: "company_news" }

如何合并具有相同键(使用日期作为键)但值不同的数组?我曾经创建过一个代码,但问题是每个键只存储一个值,但它们具有相同的键(日期)

这是我的阵列:

          {
                posting_date: "2017-08-08 00:00:00",
                id: 1,
                title: "activity 1",
                category: "company_news"
          },
          {
                posting_date: "2017-08-08 00:00:00",
                id: 6,
                title: "testing",
                category: "building_process_update"
          },
          {
                posting_date: "2017-08-08 00:00:00",
                id: 7,
                title: "ttest1",
                category: "company_news"
          },
这是我的密码:

    foreach ($result_post as $key => $value){
        $year = date('Y',strtotime($value['posting_date']));
        $month = date('M',strtotime($value['posting_date']));
        $day = date('d',strtotime($value['posting_date']));
        // $result[$year] = $value['posting_date'];
        $data = [$value['title']];
        $side_bar_date[$value['category']][$year][$month][$day] = ['id'=>$value['id'],'title'=>$value['title']];

    }
但结果是

   building_process_update: {
             2017: {
                 Aug: {
                    08: {
                       id: 6,
                       title: "testing"
                    }
                 }
             }

您的
覆盖
日期
因此。您需要将该值按如下所示的
数组
[]
键推入

$side_bar_date[$value['category']][$year][$month][$day][] = ['id'=>$value['id'],'title'=>$value['title']];

您的
覆盖
日期
因此。您需要将该值按如下所示的
数组
[]
键推入

$side_bar_date[$value['category']][$year][$month][$day][] = ['id'=>$value['id'],'title'=>$value['title']];
请尝试下面的代码

foreach ($result_post as $key => $value){
        $year = date('Y',strtotime($value['posting_date']));
        $month = date('M',strtotime($value['posting_date']));
        $day = date('d',strtotime($value['posting_date']));
        // $result[$year] = $value['posting_date'];
        $data = [$value['title']];
        $side_bar_date[$value['category']][$year][$month][$day][] = array('id'=>$value['id'],'title'=>$value['title']);
    }
这在我的情况下有效。

请尝试下面的代码

foreach ($result_post as $key => $value){
        $year = date('Y',strtotime($value['posting_date']));
        $month = date('M',strtotime($value['posting_date']));
        $day = date('d',strtotime($value['posting_date']));
        // $result[$year] = $value['posting_date'];
        $data = [$value['title']];
        $side_bar_date[$value['category']][$year][$month][$day][] = array('id'=>$value['id'],'title'=>$value['title']);
    }

这在我的情况下起作用。

发布预期结果我已经编辑了确切的输出发布预期结果我已经编辑了确切的输出很高兴它能帮助你@clienGlad it help you@clien