douxiajia6309 2019-01-24 06:51
浏览 31
已采纳

令人困惑的多维数组我有一个数组,我想生成新的数组与新的密钥是时间戳

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"
}
}
}
  • 写回答

1条回答 默认 最新

  • dongxin8709 2019-01-24 07:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错