php - 对多维数组感到困惑我有一个数组,我想用新键生成新数组,时间戳为
问题描述
对多维数组感到困惑,我有一个数组,我想用新键生成新数组,时间戳是时间戳。并且与该日期相关的所有日期都应在该键数组下。我确实尝试在数组中使用,数组键存在很多我尝试过的东西,但我无法从我的工作中得到正确的响应帮助我解决这个问题。先感谢您
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"
}
}
}
解决方案
循环数组并使用 date() 使其具有关联性。
foreach($arr as $sub){
$new[date("d", strtotime($sub["timestamp"]))][] = $sub;
}
这将循环所有项目并解析日期并将其用作关联数组中的键。
推荐阅读
- javascript - 重构 Object.keys(object).map 以获取嵌套数组值。JavaScript
- javascript - axios.get forEach 仅显示 1 个结果
- node.js - socketio 失败:在生产环境建立连接之前关闭 WebSocket
- ms-access - 具有多个条件的访问中的 if 语句
- laravel - 如何在我的 API 列中添加类似的数字?
- react-native - 使用远程 Mac 设置 React Native
- c - 为什么信号处理程序在他的方法体中调用 signal()?
- azure-devops - AzureDevops 到 Jira 的集成
- reactjs - 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。检查 `Body` 的渲染方法。但我已经有了钥匙
- sms - AT 命令中的丹麦字母