我对多维数组感到困惑,我有一个数组,我想用时间戳作为新键生成新数组。并且与该日期相关的所有日期都应在该键数组下。我确实尝试在array中使用,array key存在很多我尝试过的东西,但是我无法从工作中获得正确的响应,这有助于我解决此问题。预先谢谢
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"
}
}
}
答案 0 :(得分:0)
循环数组,并使用date()使其具有关联性。
foreach($arr as $sub){
$new[date("d", strtotime($sub["timestamp"]))][] = $sub;
}
这将循环所有项目并解析日期,并将其用作关联数组中的键。