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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题