按相同对象数排序PHP数组

按相同对象数排序PHP数组,php,arrays,Php,Arrays,是否仍要以这种方式订购阵列?例如,如果我有这个数组: $array = array("foo", "bar", "item", "item", "foo", "foo"); 我想把它订成“foo”,“foo”,“foo”,“item”,“item”,“bar”有什么办法吗?这样行吗 $array1 = array_count_values($array); arsort($array1); var_dump($array1); 我会给你 array(3) { ["foo"]=>

是否仍要以这种方式订购阵列?例如,如果我有这个数组:

$array = array("foo", "bar", "item", "item", "foo", "foo");
我想把它订成“foo”,“foo”,“foo”,“item”,“item”,“bar”有什么办法吗?

这样行吗

$array1 = array_count_values($array);
arsort($array1);
var_dump($array1);
我会给你

array(3) {
  ["foo"]=>
  int(3)
  ["item"]=>
  int(2)
  ["bar"]=>
  int(1)
}
或者你需要它们作为重复值吗?如果是,您可以选择以下方式:

usort($array,create_function('$a,$b',
    'return $GLOBALS["array1"][$a]<$GLOBALS["array1"][$b];'));
usort($array,create_函数(“$a,$b”,

'return$GLOBALS[“array1”][$a]这是一个非常不寻常的排序过程,它是最简单的两步或三步排序过程

首先对不同的对象进行计数,然后对对象计数进行排序,并从中生成已排序的对象数组。

可能会起作用。不过这很方便。通过需要进行的计算,这可能会更清晰、更有效。如果有大量重复值(100+)您可能还想考虑使用for 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
}
函数getSortedGroupArray($array){ $return=array(); $values=数组\计数\值($array); 排序($value); foreach($count=>$value的值){ 对于($i=0;$i<$count;++$i){ $return[]=$value; } } return$return }
首先,您必须计算每个值()的出现次数,然后使用根据您的条件对元素进行排序:

<?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);
让我们试试这个:

// 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's 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函数来缩短,但您知道必须做什么。

您可以使用以下函数按值在数组中出现的频率排序:

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');
}
这将产生

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
)

我可以看到它们是分组的。是什么决定了foo$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);
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
)