在PHP中使用保留键反转关联数组 [英] Reverse an associative array with preserving keys in PHP

查看:122
本文介绍了在PHP中使用保留键反转关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了半个小时,但没有找到解决方法.

I spent half an hour but I haven't found a solution.

以下数组示例:

array(14) {
  ["label_text"]=> string(10) "Label text"
  ["e-mail"]=> string(6) "E-Mail"
  ["company"]=> string(7) "Company"
  ["last_name"]=> string(9) "Last name"
  ["first_name"]=> string(10) "First name"
}

我想做的就是反转元素,所以结果是这样:

What I want to do is just reverse the elements, so that the result is this:

array(14) {
  ["first_name"]=> string(10) "First name"
  ["last_name"]=> string(9) "Last name"
  ["company"]=> string(7) "Company"
  ["e-mail"]=> string(6) "E-Mail"
  ["label_text"]=> string(10) "Label text"
}

为此必须有一个本地php,但是我认为我是盲目的.我只是不知道要使用哪个功能.

There must be a native php for this, but I think I'm blind. I just don't know which function to use.

任何帮助表示赞赏!

推荐答案

使用 array_reverse().

array array_reverse(array $ array [,bool $ preserve_keys = false])

array array_reverse ( array $array [, bool $preserve_keys = false ] )

获取一个输入数组并返回一个新数组,其顺序为 元素颠倒了.

Takes an input array and returns a new array with the order of the elements reversed.

注意:请确保您已阅读有关该函数第二个参数的文档.

Note: make sure you read the documentation about the 2nd argument of said function.

这篇关于在PHP中使用保留键反转关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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