簡體   English   中英

PHP:如何通過計算日期的出現從另一個數組創建一個新的數據數組?

[英]PHP: How can I create a new array of data from another array by counting the occurrence of dates?

我需要你的幫助。 我想從對象的多維數組中准備一些數據以與 ChartJS 數據結構相等:

https://www.chartjs.org/docs/master/general/data-structures/

因此,例如這里的數組:

$source = [
    [
        'id'           => '123',
        'date_created' => '2020-10-19 22:50:46'
    ],
    [
        'id'           => '456',
        'date_created' => '2020-10-19 13:50:19'
    ],
    [
        'id'           => '276',
        'date_created' => '2020-09-19 11:50:46'
    ],
    [
        'id'           => '846',
        'date_created' => '2020-09-19 21:50:46'
    ],
    [
        'id'           => '543',
        'date_created' => '2020-10-18 22:50:46'
    ]
];

需要轉化為:

$dest = [
    '2020-10-19' => '2',
    '2020-09-19' => '2',
    '2020-10-18' => '1'
];

因此,從邏輯上講,函數只需要按日期(而不是時間)來計算數組中時間戳的出現次數。 之后,必須創建一個新數組,以日期為鍵,並將其出現的次數作為值。

我試過用這個:

error_log( print_r( array_count_values( array_flip( array_column( $source, 'date_exported' ) ) ), true ) );

但這僅返回列出現次數的數組。 那么我怎樣才能以正確的方式處理這個問題呢?

嘗試這個。

<?php

// Your input array.
$source = [
    [
        'id'           => '123',
        'date_created' => '2020-10-19 22:50:46'
    ],
    [
        'id'           => '456',
        'date_created' => '2020-10-19 13:50:19'
    ],
    [
        'id'           => '276',
        'date_created' => '2020-09-19 11:50:46'
    ],
    [
        'id'           => '846',
        'date_created' => '2020-09-19 21:50:46'
    ],
    [
        'id'           => '543',
        'date_created' => '2020-10-18 22:50:46'
    ]
];

// Output array.
$output = [];

// Loop over each element and extract its key.
foreach ($source as $element)
{
    // Turn date string into DateTime object.
    $dateTime = new DateTime($element['date_created']);
    // Get the intended key in Y-m-d format.
    $key = $dateTime->format('Y-m-d'); 
    
    // New key? Initialize to 1.
    if (!isset($output[$key]))
        $output[$key] = 1;
    else
        // Existing key, increment.
        $output[$key]++;
}

var_dump($output);
/*
array(3) {
  ["2020-10-19"]=>
  int(2)
  ["2020-09-19"]=>
  int(2)
  ["2020-10-18"]=>
  int(1)
}
*/

或者

// Output array.
$output = [];

foreach ($source as $element)
{
    $key = explode(' ', $element['date_created'])[0];
    if (isset($output[$key]))
        $output[$key]++;
    else
        $output[$key] = 1;
}

var_dump($output);
/*
array(3) {
  ["2020-10-19"]=>
  int(2)
  ["2020-09-19"]=>
  int(2)
  ["2020-10-18"]=>
  int(1)
}
*/

或者,作為單襯:

// Output array.
$output = [];

var_dump(array_count_values(array_map(function ($element) { return explode(' ', $element)[0]; }, array_column($source, 'date_created'))));
/*
array(3) {
  ["2020-10-19"]=>
  int(2)
  ["2020-09-19"]=>
  int(2)
  ["2020-10-18"]=>
  int(1)
}
*/

如果您映射數組並在空間上explode ,那么您可以array_count_values

$result = array_count_values(array_map(function($v) {
                                           return explode(' ', $v['date_created'])[0];
                                       }, $source));

顯然有一些函數可以處理日期,但鑒於這種格式,它比轉換為時間戳然后將其格式化為日期要短。

但是,如果這是來自數據庫,那么最好只在數據部分選擇COUNT

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM