我有containts这样逗号分隔值的变量:$str = "1,5";
阵列替换数字和关联
为了将其转换为阵列我有以下代码:
$str = "1,5";
$replacements = explode(',', $str);
现在阵列看起来像以下:
Array
(
[0] => 1
[1] => 5
)
我有这样的另一个数组:$base = array('1'=>'Bread','5'=>'Butter');
我所试图做的是通过关联数组的值替换数字阵列的值高于基于关联数组的key
和数字阵列的价值,它应该看起来像以下:
// This is what I am trying to achive
Array
(
[0] => Bread // because 1 = Bread in the assoc. array
[1] => Butter // because 5 = Butter in the assoc. array
)
为了实现这一目标,我想下面的代码:
$str = "1,5";
$replacements = explode(',', $str);
$base = array('1'=>'Bread','5'=>'Butter');
$basket = array_replace($base, $replacements);
print_r($basket);
但它给我下面的输出:
Array
(
[1] => 5
[5] => Butter
[0] => 1
)
你能告诉我如何解决这个问题吗?
所以,你要通过$替代排序$基地? – Orangepill