S
S
Sergey questions2018-07-17 17:13:52
PHP
Sergey questions, 2018-07-17 17:13:52

How can array values ​​be compared, counted, and sorted out?

Hello.
There is such a php array (its print_r result):

Array result
Array
(
    [0] => Array
        (
            [itemid] => 11
            [kolvo] => 1
        )

    [1] => Array
        (
            [itemid] => 4309
            [kolvo] => 1
        )

    [2] => Array
        (
            [itemid] => 11
            [kolvo] => 1
        )

    [3] => Array
        (
            [itemid] => 4309
            [kolvo] => 1
        )

    [4] => Array
        (
            [itemid] => 11
            [kolvo] => 1
        )

    [5] => Array
        (
            [itemid] => 4309
            [kolvo] => 1
        )

    [6] => Array
        (
            [itemid] => 2689
            [kolvo] => 1
        )

    [7] => Array
        (
            [itemid] => 4445
            [kolvo] => 1
        )

    [8] => Array
        (
            [itemid] => 10
            [kolvo] => 1
        )

    [9] => Array
        (
            [itemid] => 3324
            [kolvo] => 1
        )

    [10] => Array
        (
            [itemid] => 3372
            [kolvo] => 1
        )

)


It is created like this in the engine:
foreach($items as $item) {
  $massiv[] = array(
    'itemid'=>$item[id],
    'kolvo'=>1
  );
}

You need to compare the results by the key [itemid] and, if they match, add them [kolvo] and sort them from the highest value [kolvo] to the lowest. those. array like this:
What result do you need
Array
(
    [0] => Array
        (
            [itemid] => 11
            [kolvo] => 3
        )

    [1] => Array
        (
            [itemid] => 4309
            [kolvo] => 3
        )

    [2] => Array
        (
            [itemid] => 2689
            [kolvo] => 1
        )

    [3] => Array
        (
            [itemid] => 4445
            [kolvo] => 1
        )

    [4] => Array
        (
            [itemid] => 10
            [kolvo] => 1
        )

    [5] => Array
        (
            [itemid] => 3324
            [kolvo] => 1
        )

    [6] => Array
        (
            [itemid] => 3372
            [kolvo] => 1
        )

)


I've been struggling for an hour now, I can't bring it to mind, I appeal to you uv. masters, tell me how to do it right

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2018-07-17
@VladimirAndreev

foreach($items as $item) {
  
  if(!isset($result[$item['id']]) { 
    $result[$item['id']] = 0;
  }
  
  $result[$item['id']]++;
}

arsort($result);

S
Sergey questions, 2018-07-17
@smartycms

I resolved the issue myself.

Decision
// Для примера
$items = array (
  array('id'=>3249),
  array('id'=>11),
  array('id'=>3249),
  array('id'=>134),
  array('id'=>11),
  array('id'=>11),
  array('id'=>542),
  array('id'=>542),
  array('id'=>542),
  array('id'=>542),
  array('id'=>542),
  array('id'=>2546),
  array('id'=>3849),
  array('id'=>3249),
  array('id'=>3249)
);
// End для примера


$result = array();

foreach($items as $item){
  $result[] = array(
    'kolvo'=>1,
    'itemid'=>$item['id']
  );
}

$grouped = [];
foreach ($result as $r) {
  $key = $r['itemid'];
  if (!isset($grouped[$key])) {
    $grouped[$key] = $r;
  } else {
    $grouped[$key]['kolvo'] += $r['kolvo'];
  }
}

sort($grouped);
natsort($grouped);
$grouped = array_reverse($grouped);

print_r($grouped);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question