$data = [/* your data */];
uasort($data, function($a, $b) {
preg_match("/\d+/", $a['price'], $matchesA);
preg_match("/\d+/", $b['price'], $matchesB);
return (int)$matchesB[0] <=> (int)$matchesA[0];
});
$result = array_pop($data);
Добавлено (08.08.2020) Из-за комментария vivek_23 я добавил второе решение:
$result = array_reduce($price, 'getMinimumPrice', null);
function getMinimumPrice(?array $itemA, array $itemB): array
{
if (is_null($itemA)) {
return $itemB;
}
return getPrice($itemA) < getPrice($itemB)
? $itemA
: $itemB;
}
function getPrice(array $item): int
{
preg_match("/\d+/", $item['price'], $matches);
return $matches[0];
}
А также проверяю разницу в скорости:
$primary = [
[
'name' => 'Hair Transplantation',
'price' => '€ 1000 - 4000',
],
[
'name' => 'Rhinoplasty',
'price' => '€ 2500',
],
[
'name' => 'Otoplasty',
'price' => '€ 1000',
],
/* ... 155 items */
];
function getMinimumPrice(?array $itemA, array $itemB): array
{
if (is_null($itemA)) {
return $itemB;
}
return getPrice($itemA) < getPrice($itemB)
? $itemA
: $itemB;
}
function getPrice(array $item): int
{
preg_match("/\d+/", $item['price'], $matches);
return $matches[0];
}
$timeRepeat = 1000;
$reduce = 0;
for ($i = 0; $i < $timeRepeat; $i++) {
$start = microtime(true);
$price = $primary;
array_reduce($price, 'getMinimumPrice', null);
$reduce += microtime(true) - $start;
}
$uasort = 0;
for ($i = 0; $i < $timeRepeat; $i++) {
$start = microtime(true);
$price = $primary;
uasort($price, function($a, $b) {
preg_match("/\d+/", $a['price'], $matchesA);
preg_match("/\d+/", $b['price'], $matchesB);
return (int)$matchesB[0] <=> (int)$matchesA[0];
});
array_pop($price);
$uasort += microtime(true) - $start;
}
print_r([
'uasort' => $uasort,
'reduce' => $reduce,
'difference' => round($uasort / $reduce, 12),
]);
Мои результаты :
Array (
[uasort] => 8.0096476078033
[reduce] => 2.1610336303711
[difference] => 3.706396557294
)