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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题