PHP Array Unique Sort Regular not working as expected

I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.

I'm trying to filter out the redundant data.

Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.

I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.

My array

Array
(
    [0] => Array
        (
            [Order] => Array
                (
                    [PO] => TR11214
                    [OrderID] => 242856952012
                )
        [Transaction] => Array
            (
                [TransPO] => TR11211
                [TransactionPrice] => 91.17
            )

    )

[1] => Array
    (
        [Order] => Array
            (
                [PO] => TR11214
                [OrderID] => 242856952012
            )

        [Transaction] => Array
            (
                [TransPO] => TR11212
                [TransactionPrice] => 180.41
            )

    )

[2] => Array
    (
        [Order] => Array
            (
                [PO] => TR11214
                [OrderID] => 242856952012
            )

        [Transaction] => Array
            (
                [TransPO] => TR11213
                [TransactionPrice] => 209.99
            )

    )

)

The final array I need will look something like this.

Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)

[Transaction] => Array
    (
        [0] => Array
            (
                [TransPO] => TR11211
                [TransactionPrice] => 91.17
            )

        [1] => Array
            (
                [TransPO] => TR11212
                [TransactionPrice] => 180.41
            )

        [2] => Array
            (
                [TransPO] => TR11213
                [TransactionPrice] => 209.99
            )

    )

)

I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.

my code:

$myarray = array(
0 => array(
“Order” => array(“PO” => “TR11214”, “OrderID” => 242856952012),
“Transaction” => array(“TransPO” => “TR11211”, “TransactionPrice” => 91.17)
),
1 => array(
“Order” => array(“PO” => “TR11214”, “OrderID” => 242856952012),
“Transaction” => array(“TransPO” => “TR11212”, “TransactionPrice” => 180.41)
),
2 => array(
“Order” => array(“PO” => “TR11214”, “OrderID” => 242856952012),
“Transaction” => array(“TransPO” => “TR11213”, “TransactionPrice” => 209.99)
)
);

print_r(array_unique($myarray, SORT_REGULAR));


#php #arrays

4 Likes12.50 GEEK