根据日期对数组中的项目进行分组

时间:2017-11-16 19:29:50

标签: php arrays

我有以下数组:

C:\software\data-integration>Spoon.bat
DEBUG: Using JAVA_HOME
DEBUG: _PENTAHO_JAVA_HOME=C:\Program Files\Java\jre1.8.0_152
DEBUG: _PENTAHO_JAVA=C:\Program Files\Java\jre1.8.0_152\bin\javaw.exe

C:\software\data-integration>start "Spoon" "C:\Program Files\Java\jre1.8.0_152\bin\javaw.exe"  "-Xms1024m" "-Xmx1024m" "-XX:MaxPermSize=256m" "-Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2" "-Djava.library.path=libswt\win64" "-DKETTLE_HOME=C:\software\data-integration\" "-DKETTLE_REPOSITORY=" "-DKETTLE_USER=" "-DKETTLE_PASSWORD=" "-DKETTLE_PLUGIN_PACKAGES=" "-DKETTLE_LOG_SIZE_LIMIT=" "-DKETTLE_JNDI_ROOT=" -jar launcher\pentaho-application-launcher-8.0.0.0-28.jar -lib ..\libswt\win64

我需要根据日期对此数组进行分组。

例如,具有相同日期(而非小时)的国家/地区应归为一类。

我尝试了什么:

Array
(
    [0] => Array
        (
            [Date] => 2017-11-01 09:15:02
            [Country] => France
        )
    [1] => Array
        (
            [Date] => 2017-11-01 04:45:01
            [Country] => Spain
        )
    [1] => Array
        (
            [Date] => 2017-11-03 04:45:01
            [Country] => Italy
        )
    [1] => Array
        (
            [Date] => 2017-11-04 04:45:01
            [Country] => Canada
        )
)

你能帮我找到我错过的东西吗?

感谢。

3 个答案:

答案 0 :(得分:0)

将密钥设置为仅 mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 格式的日期或不包含时间的内容。此外,创建一个新阵列,不要添加到现有阵列:

Y-m-d

答案 1 :(得分:0)

试试这个。

foreach($array as $arr){
    $ex = explode(' ', $arr['Date']);//explode the dates via space and the first element will be the date like 2017-11-01
    $newarr[$ex[0]][] = $arr['Country'];
}

输出

Array
(
    [2017-11-01] => Array
        (
            [0] => France
            [1] => Spain
        )
    [2017-11-03] => Array
        (
            [0] => Italy
        )
    [2017-11-04] => Array
        (
            [0] => Canada
        )

)

我希望这会对你有所帮助。

答案 2 :(得分:0)

$inputArray = array();
foreach ($inputArray as $data) {
   if (isset($array[$Date])) {
      $outputArray[date('Y-m-d', strtotime($data['Date']))][] = $data;
   }else{
      $outputArray['notSet'][] = $data;
   }
}