什么是最优雅的方式来重新排列关联数组? [英] Whats the most elegant way to rearrange an associative array?

查看:98
本文介绍了什么是最优雅的方式来重新排列关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个关联数组

  $哈希['水果'] ='苹果';
$哈希['名称'] ='杰夫';
$哈希['车'] ='福特';

和您不能更改在其中创建这些变量的顺序。因此汽车总是添加到名称后阵列等什么是prettiest的方式来增加/移动车到关联数组,而不是末端(默认)?

的开始
解决方案

  $哈希=阵列('车'=>'福特')+ $哈希;

Suppose you have an associative array

$hash['Fruit'] = 'Apple';
$hash['Name'] = 'Jeff';
$hash['Car'] = 'Ford';

and you cannot change the order in which these variables are created. So Car is always added to the array after Name, etc. What's the prettiest way to add/move Car to the beginning of the associative array instead of the end (default)?

解决方案

$hash = array('Car' => 'Ford') + $hash;

这篇关于什么是最优雅的方式来重新排列关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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