令人困惑的多维数组我有一个数组,我想生成新的数组与新的密钥是时间戳
问题描述:
Confusing about multidimensional array I have an array and I want to generate new array with new key with is timestamp. and related to that date all date should be under that key array. I did try using in array , array key exist many things I did tried but I am not able to get correct response from my work help me to solve this problem. Thank You in advance
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"
}
}
}
答
Loop the array and use date() to make it associative.
foreach($arr as $sub){
$new[date("d", strtotime($sub["timestamp"]))][] = $sub;
}
this will loop all items and parse the date and use that as the key in an associative array.