php中的反向数组 [英] Reverse array in php

查看:29
本文介绍了php中的反向数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

array(7) {
  [0]=> array(2) { ["id"]=> string(1) "9"  ["roi"]=> float(0)    }
  [1]=> array(2) { ["id"]=> string(1) "1"  ["roi"]=> float(0)    }
  [2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0)    }
  [3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0)    }
  [4]=> array(2) { ["id"]=> string(1) "4"  ["roi"]=> float(0)    }
  [5]=> array(2) { ["id"]=> string(1) "5"  ["roi"]=> float(141)  }
  [6]=> array(2) { ["id"]=> string(1) "6"  ["roi"]=> float(2600) }
}

我只想颠倒这个,所以 id 6(具有 2600 的 roi)在数组中排在第一位.

I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.

我该怎么做?array_reverse()rsort() 在这种情况下不起作用

How can I do this? array_reverse() and rsort() does not work in this case

推荐答案

http://php.net/manual/en/function.array-reverse.php:

$newArray = array_reverse($theArray, true);

重要的部分是 true 参数,它保留了密钥.

The important part is the true parameter, which preserves the keys.

不相信?您可以在这个键盘示例上看到它的实际效果.

Not convinced? You can see it in action on this codepad exampole.

这篇关于php中的反向数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆