<span style="text-wrap-mode: nowrap;"> uasort排序无效,请教大家,我的代码有什么问题吗?代码如下请帮忙看看</span>
<span style="text-wrap-mode: nowrap;"> public function getIndustry() {</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> $res = Db::name('XXXX')</span>
<span style="text-wrap-mode: nowrap;"> ->where(['status'=>'normal'])</span>
<span style="text-wrap-mode: nowrap;"> ->select();</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> if (!$res) {</span>
<span style="text-wrap-mode: nowrap;"> return $this->ok('请求成功', []);</span>
<span style="text-wrap-mode: nowrap;"> }</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> $industryCat1 = [];</span>
<span style="text-wrap-mode: nowrap;"> $industryCat2 = [];</span>
<span style="text-wrap-mode: nowrap;"> $industryCat3 = [];</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> foreach ($res as $key => $item) { // 记录键值</span>
<span style="text-wrap-mode: nowrap;"> if ($item['level'] === 1) {</span>
<span style="text-wrap-mode: nowrap;"> $industryCat1[$item['id']] = ['name' => $item['name'], 'weigh' => $item['weigh']];</span>
<span style="text-wrap-mode: nowrap;"> $industryCat2[$item['id']] = [];</span>
<span style="text-wrap-mode: nowrap;"> } else if ($item['level'] === 2) {</span>
<span style="text-wrap-mode: nowrap;"> $industryCat2[$item['pid']][$item['id']] = ['name' => $item['name'], 'weigh' => $item['weigh']];</span>
<span style="text-wrap-mode: nowrap;"> $industryCat3[$item['id']] = [];</span>
<span style="text-wrap-mode: nowrap;"> } else if ($item['level'] === 3) {</span>
<span style="text-wrap-mode: nowrap;"> $industryCat3[$item['pid']][$item['id']] = ['name' => $item['name'], 'weigh' => $item['weigh']];</span>
<span style="text-wrap-mode: nowrap;"> }</span>
<span style="text-wrap-mode: nowrap;"> }</span>
<span style="text-wrap-mode: nowrap;">// 对 industryCat1 按 weigh 降序排序</span>
<span style="text-wrap-mode: nowrap;">uasort($industryCat1, function ($a, $b) {</span>
<span style="text-wrap-mode: nowrap;"> if ($a['weigh'] == $b['weigh']) {</span>
<span style="text-wrap-mode: nowrap;"> return 0;</span>
<span style="text-wrap-mode: nowrap;"> }</span>
<span style="text-wrap-mode: nowrap;"> return ($a['weigh'] < $b['weigh'])? -1 : 1;</span>
<span style="text-wrap-mode: nowrap;">});</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> $res = [</span>
<span style="text-wrap-mode: nowrap;"> 'industryCat1' => $industryCat1,</span>
<span style="text-wrap-mode: nowrap;"> 'industryCat2' => $industryCat2,</span>
<span style="text-wrap-mode: nowrap;"> 'industryCat3' => $industryCat3,</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> ];</span>
<span style="text-wrap-mode: nowrap;"></span>
<span style="text-wrap-mode: nowrap;"> return $this->ok('请求成功', $res);</span>
<span style="text-wrap-mode: nowrap;">}</span>
<span style="text-wrap-mode: nowrap;"><img src="/data/upload/article/20241229/1735468157555507.png" title="1735468157555507.png" alt="image.png"/></span>
对 industryCat1 按 weigh 降序排序,保留键名,但是始终无法按 weigh 降序排序,仍然按键名升序排序,请教大家,这里是不是我写的有问题?万分感谢!
uasort 这里可能有些问题,修改下面的试下 uasort ($industryCat1. function ($a, $b) { if ($a ['weigh'] == $b ['weigh']) { return 0; } return ($a ['weigh'] < $b ['weigh'])? 1 : -1; });
感谢回复,下面的应该怎么修改?您给的代码好像和我的一样。
看1 和-1 那里