Answer the question
In order to leave comments, you need to log in
How to sort an array by price and remove identical elements?
How to sort an array by price and remove identical elements?
Here is the array
Array
(
[0] => Array
(
[word] => автомобил
[max_bid] => 12
)
[1] => Array
(
[word] => автомобил
[max_bid] => 46
)
[2] => Array
(
[word] => автомобил
[max_bid] => 9
)
[3] => Array
(
[word] => продвижен
[max_bid] => 9
)
[4] => Array
(
[word] => автомобил
[max_bid] => 15
)
[5] => Array
(
[word] => автомобил
[max_bid] => 22
)
[6] => Array
(
[word] => продвижен
[max_bid] => 22
)
)
[0] => Array
(
[word] => автомобил
[max_bid] => 46
)
[1] => Array
(
[word] => продвижен
[max_bid] => 22
)
$rr_wordz = array();
foreach($rr_word as $row_arra){
if($row_arra['word'] !== $arr_word && $row_arra['max_bid'] > $arr_max_bid){
$arr_word = $row_arra['word'];
$arr_max_bid = $row_arra['max_bid'];
$rr_wordz[]['word'] = $row_arra['word'];
$rr_wordz[]['max_bid'] = $row_arra['max_bid'];
}
}
Array
(
[0] => Array
(
[word] => автомобил
)
[1] => Array
(
[max_bid] => 12
)
[2] => Array
(
[word] => продвижен
)
[3] => Array
(
[max_bid] => 22
)
)
Answer the question
In order to leave comments, you need to log in
$temp = array();
foreach ($rr_wordz as $row) {
if (!isset($temp[$row['word']]) || $temp[$row['word']] < $row['max_bid'])
$temp[$row['word']] = $row['max_bid'];
}
$result = array();
foreach ($temp as $key => $val)
$result[] = array('word' => $key, 'max_bid' => $val);
Where is the data from?
If from basis it would be more correct to make it in request.
$word = array();
$max_bid = array();
for ($i = 0; $i < count($rr_word); $i++) {
$word[] = $rr_word[$i]['word'];
$max_bid[] = $rr_word[$i]['max_bid'];
}
array_multisort($word, SORT_ASC, $max_bid, SORT_DESC);
$word = array_unique($word);
$result = array();
foreach ($word as $key => $value) {
$result[] = array(
'word' => $value,
'max_bid' => $max_bid[$key],
);
}
Array
(
[0] => Array
(
[word] => автомобил
[max_bid] => 46
)
[1] => Array
(
[word] => продвижен
[max_bid] => 22
)
)
$groupMakesByWord = array();
foreach ($makes as $make) {
$groupMakesByWord[$make['word']][] = $make;
}
foreach ($groupMakesByWord as $makeWord => &$makes) {
uasort($makes, function($a, $b) {
if ($a['max_bid'] == $b['max_bid']) {
return 0;
}
return $a['max_bid'] < $b['max_bid'] ? 1 : -1;
});
}
unset($makes);
$result = array();
foreach ($groupMakesByWord as $makes) {
$result[] = current($makes);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question