php - How to create a new array with keys based on calculated values of another array? -


i have array $post of following format

$post[0] = [     'id'    => '103',     'date'  => '2016-04-17 16:30:12',     'desc'  => 'content description' ];  $post[1] = [     'id'    => '102',     'date'  => '2016-04-17 12:30:12',     'desc'  => 'content description' ];  $post[2] = [     'id'    => '101',     'date'  => '2016-04-17 10:30:12',     'desc'  => 'content description' ];  $post[3] = [     'id'    => '100',     'date'  => '2016-04-16 08:30:12',     'desc'  => 'content description' ]; 

i use strtotime(date) $post unique array key, , create:

$summary['day-of-2016-04-17'] = [     'counts' => '3' ];  $summary['day-of-2016-04-16'] = [     'counts' => '1'  ]; 

where counts number of occurrence of date used key.

i need keep date unique key , time value irrelevant.

i'll need key value unix timestamp further processing.

how can implement in efficient way?

just use date key. simplest way use explode date time, first fragment (which date), , assign normal array:

$summary = []; foreach($post $value) {     $dt = explode(' ', $value['date']); // break date     $day_of = "day-of-{$dt[0]}"; // create naming key     if(!isset($summary[$day_of])) { // initialize         $summary[$day_of]['counts'] = 0;     }     $summary[$day_of]['counts']++; // increment } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -