创建基于值

时间:2015-06-29 16:02:00

标签: php mysql arrays

我有一个以下格式的数组,

Array
(
    [0] => Array
        (
            [expense_id] => 7
            [type] => frfrf
            [mode] => rfr
            [amount] => 100
            [tran_type] => expense
            [date] => 2015-06-29
        )

    [1] => Array
        (
            [expense_id] => 8
            [type] => frfrf
            [mode] => rfr
            [amount] => 100
            [tran_type] => expense
            [date] => 2015-06-29
        )

    [2] => Array
        (
            [expense_id] => 9
            [type] => frfrf
            [mode] => rfr
            [amount] => 444
            [tran_type] => expense
            [date] => 2015-06-29
        )

    [3] => Array
        (
            [expense_id] => 10
            [type] => frfrf
            [mode] => rfr
            [amount] => 1000
            [tran_type] => income
            [date] => 2015-06-29
        )

    [4] => Array
        (
            [expense_id] => 5
            [type] => frfrf
            [mode] => rfr
            [amount] => 100
            [tran_type] => expense
            [date] => 2015-06-01
        )

)

我希望根据date值合并数组,并将其存储为数组,密钥为date

Array
(
    [2015-06-29] => Array
        (
            [0] => Array
                (
                    [expense_id] => 7
                    [type] => frfrf
                    [mode] => rfr
                    [amount] => 100
                    [tran_type] => expense
                    [date] => 2015-06-29
                )

            [1] => Array
                (
                    [expense_id] => 8
                    [type] => frfrf
                    [mode] => rfr
                    [amount] => 100
                    [tran_type] => expense
                    [date] => 2015-06-29
                )

            [2] => Array
                (
                    [expense_id] => 9
                    [type] => frfrf
                    [mode] => rfr
                    [amount] => 444
                    [tran_type] => expense
                    [date] => 2015-06-29
                )

            [3] => Array
                (
                    [expense_id] => 10
                    [type] => frfrf
                    [mode] => rfr
                    [amount] => 1000
                    [tran_type] => income
                    [date] => 2015-06-29
                )

        )

    [2015-06-01] => Array
        (
            [0] => Array
                (
                    [expense_id] => 5
                    [type] => frfrf
                    [mode] => rfr
                    [amount] => 100
                    [tran_type] => expense
                    [date] => 2015-06-01
                )

        )

)

目前我正在使用foreach循环,是否有任何简单的方法来执行此操作

foreach ($d as $v) {
    $d1[$v['date']][] = $v;
}

使用mysql检索数据,也可以接受mysql查询

1 个答案:

答案 0 :(得分:1)

我只知道这种方式

$newArray = [];

foreach ($array as $subArray)
{
    $newArray[$subArray['date']][] = $subArray;
}