开关关联数组两个项目 [英] Switch two items in associative array

查看:93
本文介绍了开关关联数组两个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

$arr = array(
  'apple'      => 'sweet',
  'grapefruit' => 'bitter',
  'pear'       => 'tasty',
  'banana'     => 'yellow'
);

我想改用柚子,梨的位置,所以,阵列将处于

I want to switch the positions of grapefruit and pear, so the array will become

$arr = array(
  'apple'      => 'sweet',
  'pear'       => 'tasty',
  'grapefruit' => 'bitter',
  'banana'     => 'yellow'
)

我知道我想改用元素的键和值,有一个简单的方法来做到这一点?还是会需要一个循环+创建新阵列?

I know the keys and values of the elements I want to switch, is there an easy way to do this? Or will it require a loop + creating a new array?

感谢

推荐答案

有没有简单的方法,只是一个循环或一个新的数组定义。

There is no easy way, just a loop or a new array definition.

这篇关于开关关联数组两个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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