0

I have an array in the following format,

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
        )

)

I want merge the array based on date value and store it as an array with key as 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
                )

        )

)

Currently I'm doing it using foreach loop , is there any simple way to do this

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

Data is retrieved using mysql, mysql query is also acceptable

3
  • For what are you looking for? It's already only 3 lines, so a 1 or 0 liner ? Commented Jun 29, 2015 at 16:09
  • @Rizier123 is there in built in function? Commented Jun 29, 2015 at 16:13
  • @Rizier123 : also is there any sql query for doing this Commented Jun 29, 2015 at 16:18

1 Answer 1

1

I know only this way

$newArray = [];

foreach ($array as $subArray)
{
    $newArray[$subArray['date']][] = $subArray;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.