dongpangfu6322 2013-01-25 16:19
浏览 39
已采纳

无法使排序数组工作[重复]

Possible Duplicate:
php sort($array) not working - returning 1 instead of sorted array

I'm having a difficult time getting array sort() function to work as expected, I've also tried ksort() etc. :::

This Works Fine

foreach( (array) $query as $post ) {
   $VP = ( ( empty($PMD['vehicle_price'][0]) ) ?  $VP = 'On Request' : $VP = $PMD['vehicle_price'][0] ); 
      $vehicle_p[] = $VP;
}

$return = array_count_values($vehicle_p); //Key Value Unique ( count )

foreach($return as $value => $count) {
   <li><span class="key">' . $value . '</span> (' . $count . ')</li>
}

End Result: 480000 (1), 80000 (2), 120400 (1)

I now want to sort this so 80000 (2) is first in list folowed by 120400 (1) then 480000 (1)

As soon as I add any sort() function like below I only get an output 1

I'm not able to get this to work any help would bew appreciated

$return = array_count_values($vehicle_p); //Key Value Unique ( count )
$return = sort($return);
print_r($return);
  • 写回答

2条回答 默认 最新

  • doufan9377 2013-01-25 16:24
    关注

    If i understand correctly you want to sort the array by ascending values? if this is correct Look at asort() which will sort the array by value but keep the index/key intact

    http://php.net/manual/en/function.asort.php

    Keep in mind that the sort functions return a Boolean so don't assign them to a variable, just call the function on the array.

    asort($array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测