Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中创建关联多维数组_Php_Arrays_Sorting - Fatal编程技术网

在PHP中创建关联多维数组

在PHP中创建关联多维数组,php,arrays,sorting,Php,Arrays,Sorting,我试图创建一个数组,其中月份是键,每个键包含一个或多个日期。我从下面看起来像$arr的数组开始。旁注:我不控制原始数组的结构,因为它来自API。我只是添加了下面的$arr,以便于人们理解和调试 因为我需要以不同的方式显示日期,所以我需要构造一个数组,其中包含月份名称和格式化日期: $sorted = array(); // This is the array I will return later. foreach ( $arr as $day ) { setlocale(LC_TIME

我试图创建一个数组,其中月份是键,每个键包含一个或多个日期。我从下面看起来像$arr的数组开始。旁注:我不控制原始数组的结构,因为它来自API。我只是添加了下面的$arr,以便于人们理解和调试

因为我需要以不同的方式显示日期,所以我需要构造一个数组,其中包含月份名称和格式化日期:

$sorted = array(); // This is the array I will return later.
foreach ( $arr as $day ) {
    setlocale(LC_TIME, 'sv_SE');
    $month          = strftime( '%B',   strtotime( $day['date'] ) );
    $display_date   = trim( strftime( '%e %b', strtotime( $day['date'] ) ) );
}
到目前为止,我在这里所做的一切都失败了。老实说,我甚至记不起所有的方法。我最后一次尝试是(在foreach()中):

其中的var_dump()生成了一个枚举数组:

    array (size=4)
    0 => 
        array (size=1)
        'December' => string '7 Dec' (length=5)
    1 => 
        array (size=1)
        'December' => string '19 Dec' (length=6)
    2 => 
        array (size=1)
        'Januari' => string '3 Jan' (length=5)
    3 => 
        array (size=1)
        'Januari' => string '18 Jan' (length=6)
我试图实现的是:

所有
$display\u date
都应位于其
$month
键下。
$month
键必须是唯一的,并且包含该月的所有日期


感谢您的帮助,让我在这里找到了正确的方向,因为我觉得我在做一些根本错误的事情。

如果您在每个循环中都添加了带有月份和日期的新数组,请将
array\u push()
替换为
$sorted[$month][=$display\u date

foreach ( $arr as $day ) {
    setlocale(LC_TIME, 'sv_SE');
    $month          = strftime( '%B',   strtotime( $day['date'] ) );
    $display_date   = trim( strftime( '%e %b', strtotime( $day['date'] ) ) );

    $sorted[$month][] = $display_date;
}

print_r($sorted);
输出:

Array
(
    [december] => Array
        (
            [0] => 7 dec
            [1] => 19 dec
        )

    [januari] => Array
        (
            [0] => 3 jan
            [1] => 18 jan
        )

)

如果要在每个循环中添加带有月份和日期的新数组,请将
array\u push()
替换为
$sorted[$month][]=$display\u date

foreach ( $arr as $day ) {
    setlocale(LC_TIME, 'sv_SE');
    $month          = strftime( '%B',   strtotime( $day['date'] ) );
    $display_date   = trim( strftime( '%e %b', strtotime( $day['date'] ) ) );

    $sorted[$month][] = $display_date;
}

print_r($sorted);
输出:

Array
(
    [december] => Array
        (
            [0] => 7 dec
            [1] => 19 dec
        )

    [januari] => Array
        (
            [0] => 3 jan
            [1] => 18 jan
        )

)

这很有效。不得不说,语法感觉有点不直观,但我猜数组通常有点让人费解。谢谢这很有效。不得不说,语法感觉有点不直观,但我猜数组通常有点让人费解。谢谢