当前位置:

uasort排序无效,请教大家,我的代码有什么问题吗?

  • 作者:
    gitasp
  • 日期:
    2024/12/29
  • 所属分类:
    问答

<span style="text-wrap-mode: nowrap;">&nbsp;&nbsp;uasort排序无效,请教大家,我的代码有什么问题吗?代码如下请帮忙看看</span>

<span style="text-wrap-mode: nowrap;">&nbsp; public function getIndustry() {</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; $res = Db::name(&#39;XXXX&#39;)</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; -&gt;where([&#39;status&#39;=&gt;&#39;normal&#39;])</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; -&gt;select();</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; if (!$res) {</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;ok(&#39;请求成功&#39;, []);</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; }</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; $industryCat1 = [];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; $industryCat2 = [];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; $industryCat3 = [];</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; foreach ($res as $key =&gt; $item) {&nbsp; // 记录键值</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; if ($item[&#39;level&#39;] === 1) {</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $industryCat1[$item[&#39;id&#39;]] = [&#39;name&#39; =&gt; $item[&#39;name&#39;], &#39;weigh&#39; =&gt; $item[&#39;weigh&#39;]];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $industryCat2[$item[&#39;id&#39;]] = [];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; } else if ($item[&#39;level&#39;] === 2) {</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $industryCat2[$item[&#39;pid&#39;]][$item[&#39;id&#39;]] = [&#39;name&#39; =&gt; $item[&#39;name&#39;], &#39;weigh&#39; =&gt; $item[&#39;weigh&#39;]];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $industryCat3[$item[&#39;id&#39;]] = [];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; } else if ($item[&#39;level&#39;] === 3) {</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $industryCat3[$item[&#39;pid&#39;]][$item[&#39;id&#39;]] = [&#39;name&#39; =&gt; $item[&#39;name&#39;], &#39;weigh&#39; =&gt; $item[&#39;weigh&#39;]];</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; }</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; }</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;">&nbsp; &nbsp; if ($a[&#39;weigh&#39;] == $b[&#39;weigh&#39;]) {</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; return 0;</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; }</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; return ($a[&#39;weigh&#39;] &lt; $b[&#39;weigh&#39;])? -1 : 1;</span>

<span style="text-wrap-mode: nowrap;">});</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; $res = [</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &#39;industryCat1&#39; =&gt; $industryCat1,</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &#39;industryCat2&#39; =&gt; $industryCat2,</span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &#39;industryCat3&#39; =&gt; $industryCat3,</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; ];</span>

<span style="text-wrap-mode: nowrap;"></span>

<span style="text-wrap-mode: nowrap;">&nbsp; &nbsp; return $this-&gt;ok(&#39;请求成功&#39;, $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 [&#39;weigh&#39;] == $b [&#39;weigh&#39;]) { return 0; } return ($a [&#39;weigh&#39;] &lt; $b [&#39;weigh&#39;])? 1 : -1; });

感谢回复,下面的应该怎么修改?您给的代码好像和我的一样。

看1 和-1 那里

源码下载月排行
软件下载月排行
文章浏览月排行