Order PHP array by the number of identical objects

Is it possible to order an array this way? For example, if I had this array:

$array = array("foo", "bar", "item", "item", "foo", "foo");

And I wanted to order it so that it was "foo", "foo", "foo", "item", "item", "bar", is there any way to do this?

+3
source share
6 answers

Will it do it?

$array1 = array_count_values($array);
arsort($array1);
var_dump($array1);

will provide you

array(3) {
  ["foo"]=>
  int(3)
  ["item"]=>
  int(2)
  ["bar"]=>
  int(1)
}

or do you necessarily need them as duplicate values? if so, you can go for something like:

usort($array,create_function('$a,$b',
    'return $GLOBALS["array1"][$a]<$GLOBALS["array1"][$b];'));

This is ugly code, but demonstrates the technique. It's also easy to make this beautiful with php 5.3 closing, but I don't know if you are on 5.3. It will look like this:

$acount=array_count_values($array = array("foo", "bar", "item", "item", "foo", "foo"));
usort($array,function($a,$b) use ($acount) { return $acount[$a]<$acount[$b]; });
+4
source

(array_count_values), usort, :

<?php

$array = array('foo', 'bar', 'bar', 'foo', 'bar', 'foo', 'foobar', 'foo', 'foo', 'foobar', 'bar', 'foo');

$tmp = array_count_values($array);
usort($array, function($e1, $e2) use($tmp) {
    return $tmp[$e2] - $tmp[$e1];
});

var_dump($array);
+2

usort() . array_count_values ​​() . , , . (100+), array_fill() for:

function getSortedGroupArray($array) {
  $return = array();
  $values = array_count_values($array);
  sort($values);
  foreach($values as $count => $value) {
    for($i = 0; $i < $count; ++$i) {
      $return[] = $value;
    }
  }
  return $return
}
+1

, - .

, .

0

:

// First, lets count the number of objects  
$sort_by_term = array();
foreach($array as $string)
{
   if(isset($sort_by_term[$string]))
   {
       $sort_by_term[$string] += 1;
   }
   else
   {
       $sort_by_term[$string] = 1;
   }
}

// Next let sort them by number
$sort_by_count = array();
foreach($sort_by_term as $term => $count)
{
    $sort_by_count[$count][] = $term;
}

// Now lets combine them
$final_array = array();
foreach($sort_by_count as $count => $term)
{
    while($count > 0)
    {
        $final_array[] = $term;
        $count -= 1;
    }
}

PHP, , .

0
source

You can use the following function to sort by the frequency with which the value appears in the array:

function array_count_sort(&$array, $direction = 1)
{
    // Could do with a better way of making $counts and $dir available to the
    // sorting function, but this will do for illustrative purposes.
    global $counts, $dir; 
    $counts = array_count_values($array);
    $dir = $direction;

    if (!function_exists('array_count_sort_cmp')) {
        function array_count_sort_cmp($a, $b) {
            global $counts, $dir;

            $c = $counts[$a];
            $d = $counts[$b];

            if ($c == $d) return 0;
            return ($c < $d) ? -$dir : $dir;
        }
    }

    usort($array, 'array_count_sort_cmp');
}

And use it as follows:

$test = array("foo", "bar", "item", "item", "foo", "foo");
print_r($test);
array_count_sort($test);
print_r($test);
array_count_sort($test, -1);
print_r($test);

what will give

Array
(
    [0] => foo
    [1] => bar
    [2] => item
    [3] => item
    [4] => foo
    [5] => foo
)
Array
(
    [0] => bar
    [1] => item
    [2] => item
    [3] => foo
    [4] => foo
    [5] => foo
)
Array
(
    [0] => foo
    [1] => foo
    [2] => foo
    [3] => item
    [4] => item
    [5] => bar
)
0
source

Source: https://habr.com/ru/post/1743738/


All Articles