-1

I am facing some issue in multiple array to find height amount array.

I want to get the array that have high amount from multiple array to one new array in PHP.

If two or more arrays have same highest amount than get any array as new array.

Currently, I have two arrays having same amount but its not highest value so it will not select in the sample data.

The Output Array should be combined with the highest amount with its code and type.


Input:

(
    [0] => Array
        (
            [code] => Custom discount
            [amount] => 3514.55
            [type] => fixed_amount
        )

    [1] => Array
        (
            [code] => MerOrder300
            [amount] => 400.00
            [type] => fixed_amount
        )

    [2] => Array
        (
            [code] => MerOrder400
            [amount] => 400.00
            [type] => fixed_amount
        )

    [3] => Array
        (
            [code] => MerOrder450
            [amount] => 450.00
            [type] => fixed_amount
        )

)

Output:

( 
    [0] => Array
        (
            [code] => Custom discount
            [amount] => 3514.55
            [type] => fixed_amount
        )
)
1
  • 3
    What have you tried so far? You haven't posted any code, so we can't help you find the issue. Commented Mar 12, 2020 at 10:25

3 Answers 3

2

You can use array_column with array_keys and max

$amountArr = array_column($arr, null, 'amount');
print_r($amountArr[max(array_keys($amountArr))]);

Working example :- https://3v4l.org/d2WRf

Sign up to request clarification or add additional context in comments.

Comments

1

Rakesh's solution eliminates the possibility of multiple qualifying subarrays by using amount as new keys during the array_column() call.

There are at least a handful of different ways to perform this task. Here is just one -- extract the max value, then filter the whole array using that value.

Code: (Demo)

$array = [
    [
        'code' => 'Custom discount',
        'amount' => 3514.55,
        'type' => 'fixed_amount'
    ],
    [
        'code' => 'MerOrder300',
        'amount' => 400.00,
        'type' => 'fixed_amount'
    ],
    [
        'code' => 'MerOrder400',
        'amount' => 3514.55,
        'type' => 'fixed_amount'
    ],
    [
        'code' => 'MerOrder450',
        'amount' => 450.00,
        'type' => 'fixed_amount'
    ]
];

$max = max(
    array_column($array, 'amount')
);
var_export(
    array_filter(
        $array,
        function($subarray) use ($max) {
            return $subarray['amount'] == $max;
        }
    )
);

2 Comments

Linear / Highest performing techniques like linauror's answer can be explained in this similar pre-existing page: stackoverflow.com/q/33624879/2943403
What do you mean by "If two or more arrays have same highest amount than get any array as new array."? @Muneeb that English is not perfectly clear. If two subarrays contain the highest value, do you expect 1 or 2 subarrays in the result? Your sample input does not reflect your desire because only one subarray contains the max value.
0

You can use foreach:

$arr = [
    [
        'code' => 'Custom discount', 
        'amount' => 3514.55,
        'type' => 'fixed_amount'
    ],
    [
        'code' => 'MerOrder300', 
        'amount' => 400.00,
        'type' => 'fixed_amount'
    ],    
    [
        'code' => 'MerOrder400', 
        'amount' => 400.00,
        'type' => 'fixed_amount'
    ],
    [
        'code' => 'MerOrder450', 
        'amount' => 450.00,
        'type' => 'fixed_amount'
    ]
];

$max = $arr[0];

foreach ($arr as $line) {
    if ($line['amount'] > $max['amount']) {
        $max = $line;
    }
}

print_r($max);

1 Comment

Code-only answers are low value on Stackoverflow because they do very little to educate/empower thousands of future researchers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.