PHP 在关联数组前面加上文字键? [英] PHP prepend associative array with literal keys?

查看:21
本文介绍了PHP 在关联数组前面加上文字键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在关联数组之前添加文字键=> 值对?我知道 array_unshift() 可以使用数字键,但我希望可以使用文字键.

Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I'm hoping for something that will work with literal keys.

举个例子,我想做以下事情:

As an example I'd like to do the following:

$array1 = array('fruit3'=>'apple', 'fruit4'=>'orange');
$array2 = array('fruit1'=>'cherry', 'fruit2'=>'blueberry');

// prepend magic

$resulting_array = ('fruit1'=>'cherry', 
                    'fruit2'=>'blueberry', 
                    'fruit3'=>'apple', 
                    'fruit4'=>'orange');

推荐答案

你就不能这样做:

$resulting_array = $array2 + $array1;

?

这篇关于PHP 在关联数组前面加上文字键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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