首页 > 解决方案 > 对多维数组感到困惑我有一个数组,我想用新键生成新数组,时间戳为

问题描述

对多维数组感到困惑,我有一个数组,我想用新键生成新数组,时间戳是时间戳。并且与该日期相关的所有日期都应在该键数组下。我确实尝试在数组中使用,数组键存在很多我尝试过的东西,但我无法从我的工作中得到正确的响应帮助我解决这个问题。先感谢您

My current array which is I am getting from response is like this: 

array {
      0=> {
        "id"=> "1"
        "userId" => "bhaveshdarji386"
        "timestamp"=> "2019-01-20T08:29:48.000+0000"
        "property"=> "candidate"
        "newValue"=> "ijhgf"
      }
      1=> {
        "id"=> "2"
        "userId" => "bhaveshdarji386"
        "timestamp"=> "2019-01-20T08:29:48.000+0000"
        "property"=> "candidate"
        "newValue"=> "frtyui"
      }
      2=> {
        "id"=> "3"
        "userId" => "bhaveshdarji386"
        "timestamp"=> "2019-01-16T08:29:48.000+0000"
        "property"=> "candidate"
        "newValue"=> "kjhg"
      }
      3=> {
        "id"=> "4"
        "userId" => "bhaveshdarji386"
        "timestamp"=> "2019-01-17T08:29:48.000+0000"
        "property"=> "candidate"
        "newValue"=> "hgfd"
      }
    }

And I want to produce my new array like this

array {
20 => { 
    0 => {
    "id"=> "1"
    "userId" => "bhaveshdarji386"
    "timestamp"=> "2019-01-20T08:29:48.000+0000"
    "property"=> "candidate"
    "newValue"=> "ijhgf"
  }
    1=> {
    "id"=> "2"
    "userId" => "bhaveshdarji386"
    "timestamp"=> "2019-01-20T08:29:48.000+0000"
    "property"=> "candidate"
    "newValue"=> "frtyui"
      }
}
17 => {
0 => {
    "id"=> "4"
    "userId" => "bhaveshdarji386"
    "timestamp"=> "2019-01-17T08:29:48.000+0000"
    "property"=> "candidate"
    "newValue"=> "hgfd"
}
}
16=> {
    0 => {
    "id"=> "3"
    "userId" => "bhaveshdarji386"
    "timestamp"=> "2019-01-16T08:29:48.000+0000"
    "property"=> "candidate"
    "newValue"=> "kjhg"
}
}
}

标签: php

解决方案


循环数组并使用 date() 使其具有关联性。

foreach($arr as $sub){
    $new[date("d", strtotime($sub["timestamp"]))][] = $sub;
}

这将循环所有项目并解析日期并将其用作关联数组中的键。


推荐阅读