I have this array:
$array_1 = [
['model' => 'iPhone 12', 'grade' => 'A', 'price' => '100'],
['model' => 'iPhone 12', 'grade' => 'A', 'price' => '95'],
['model' => 'iPhone 12', 'grade' => 'B', 'price' => '85'],
['model' => 'iPhone 12', 'grade' => 'C', 'price' => '75'],
];
I would like to merge the values of the grade
and price
into its own key value pair and make it look like this:
$array_2 = [
['model' => 'iPhone 12', 'A' => '100'],
['model' => 'iPhone 12', 'A' => '95'],
['model' => 'iPhone 12', 'B' => '85'],
['model' => 'iPhone 12', 'C' => '75']
];
I would then like to sort the data by looking at the arrays with similar grades and keep only the array with the lowest price. So in the end the array should look something like this:
$array_3 = [
['model' => 'iPhone 12', 'A' => '95'],
['model' => 'iPhone 12', 'B' => '85'],
['model' => 'iPhone 12', 'C' => '75']
];
How can I go about doing this?