How can i filter in php array of objects?

i have an array of objects in a php code. I want to filter to be an other array of object forms.

in javascript we can use array.filter(..) , but i want to build this in my php project

[
    {
        "date": "2019-01-01",
        "branch": "Office",
        "PaymentAccount": "1 2 01 05",
        "Supplier": "Health Company, UD",
        "AccountDetail": "1 01 01 02",
        "Amount": "100.000",
        "Description": "Test Cash Out"
    },
    {
        "date": "2019-01-01",
        "branch": "Office",
        "PaymentAccount": "1 2 01 05",
        "Supplier": "Health Company, UD",
        "AccountDetail": "1 01 01 02",
        "Amount": "100.000",
        "Description": "Test Cash Out"
    },
    {
        "date": "2019-01-02",
        "branch": "Office",
        "PaymentAccount": "1 2 01 05",
        "Supplier": "Health Company, UD",
        "AccountDetail": "1 01 01 02",
        "Amount": "100.000",
        "Description": "Test Cash Out"
    },
    {
        "date": "2019-01-02",
        "branch": "Office",
        "PaymentAccount": "1 2 01 05",
        "Supplier": "Health Company, UD",
        "AccountDetail": "1 01 01 02",
        "Amount": "100.000",
        "Description": "Test Cash Out"
    }
]

and i want to filter my array to new forms like this in PHP code. The key of >the filter is date, branch, payment account, and supplier.

[
    {
        "date": "2019-01-01",
        "branch": "Office",
        "PaymentAccount": "1 2 01 05",
        "Supplier": "Health Company",
        "detail":[
            {
                "accountDetail": "1 01 01 02",
                "amount": "100.000",
                "description": "Test Cash Out"
            },
            {
                "accountDetail": "1 01 01 02",
                "amount": "100.000",
                "description": "Test Cash Out"
            }
        ]
    },
    {
        "date": "2019-01-02",
        "branch": "Office",
        "PaymentAccount": "1 2 01 05",
        "Supplier": "Health Company",
        "detail":[
            {
                "accountDetail": "1 01 01 02",
                "amount": "100.000",
                "description": "Test Cash Out"
            },
            {
                "accountDetail": "1 01 01 02",
                "Amount": "100.000",
                "description": "Test Cash Out"
            }
        ]
    }
]

2 answers

  • answered 2019-09-10 05:01 Naveen S

    I don't think it is a filter that you need. You can try for array_map() which will walk you through each array and you can change the structure as you desire.

    check this https://www.php.net/manual/en/function.array-map.php

  • answered 2019-09-10 05:19 Apps Maven

    By using:- array array_filter($array, $callback_function, $flag) This built-in function in PHP is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined function or the callback function. If the callback function returns true then the current value of the array is returned into the result array otherwise not. This way the keys of the array gets preserved, i.e. the key of element in the original array and output array are same.