从数组中只获取5个元素

cup*_*kob 2 php arrays filter

我的数组设置如下:

array
  'testuri/abc' => 
    array
      'label' => string 'abc' (length=3)
      'weight' => float 5
  'testuri/abd' => 
    array
      'label' => string 'abd' (length=3)
      'weight' => float 2
  'testuri/dess' => 
    array
      'label' => string 'dess' (length=4)
      'weight' => float 2
  'testuri/gdm' => 
    array
      'label' => string 'gdm' (length=3)
      'weight' => float 2
  'testuri/abe' => 
    array
      'label' => string 'abe' (length=3)
      'weight' => float 2
  'testuri/esy' => 
    array
      'label' => string 'esy' (length=3)
      'weight' => float 2
  'testuri/rdx' => 
    array
      'label' => string 'rdx' (length=3)
      'weight' => float 3
  'testuri/tfc' => 
    array
      'label' => string 'tfc' (length=3)
      'weight' => float 3
Run Code Online (Sandbox Code Playgroud)

我想得到/过滤5个元素与bigges'权重'.有这个功能吗?

PS.我的想法是使用foreach

Gum*_*mbo 7

按权重值按降序对数组进行排序,然后获取前五个值:

function cmpByWeight($a, $b) {
    return $b['weight'] - $a['weight'];
}
uasort($array, 'cmpByWeight');
$firstFive = array_slice($array, 0, 5);
Run Code Online (Sandbox Code Playgroud)