如何用另一个角色替换我的数组中的角色?

时间:2016-01-04 14:54:35

标签: php arrays

我有一个数组 - 我的数组输出如下:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["-"] => 7
    ["FR"] => 3
)

我希望将“ - ”替换为“其他”

所以最后看起来应该是这样的:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["other"] => 7
    ["FR"] => 3
)

有人可以帮我这个吗? str_replace没跟我合作过...... 如果你能,我希望在底部有阵列的“其他”部分 - 像这样:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["FR"] => 3
    ["other"] => 7
)

谢谢:)

当前代码:

 $array_land = explode("\n", $land_exec_result);
 $count_land = array_count_values($array_land);
        arsort($count_land);
        $arr['other'] = $count_land["-"];
        unset($count_land["-"]);

但这对我没用:/

2 个答案:

答案 0 :(得分:2)

这样简单:

$array["other"] = $array["-"];
unset($array["-"]);

最后,数组将是这样的:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["FR"] => 3
    ["other"] => 7
)

答案 1 :(得分:1)

$arr['other'] = $arr['-'];
unset($arr['-']);

第一个命令将$arr['-']元素的值存储在名为$arr['other']的新元素中。当您以这种方式为具有命名索引的数组创建新元素时,新元素将自动放置在数组的末尾。

第二个命令从数组中删除$arr['-']元素。 结果将是:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["FR"] => 3
    ["other"] => 7
)