N
N
Nikolay Gorodyan2014-02-08 19:15:58
PHP
Nikolay Gorodyan, 2014-02-08 19:15:58

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
        )

)

Should work
[0] => Array
        (
            [word] => автомобил
            [max_bid] => 46
        )
[1] => Array
        (
            [word] => продвижен
            [max_bid] => 22
        )

Those. there are a bunch of words "car" but it is selected where max_bid is the most, the rest will be deleted.
PS Preferably with code.
My code ($rr_word is the array posted above).
$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'];

    }
  }

Displays
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

4 answer(s)
R
Rsa97, 2014-02-08
@WarDJ

$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);

O
OnYourLips, 2014-02-08
@OnYourLips

Where is the data from?
If from basis it would be more correct to make it in request.

F
fabrykant, 2014-02-08
@fabrykant

$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],
     );
}

succeed
Array
(
    [0] => Array
        (
            [word] => автомобил
            [max_bid] => 46
        )

    [1] => Array
        (
            [word] => продвижен
            [max_bid] => 22
        )

)

S
Shahelm, 2014-02-08
@Shahelm

$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 question

Ask a Question

731 491 924 answers to any question