如何用另一个字符替换数组中的一个字符?
如何用另一个字符替换数组中的一个字符?
我有一个数组 - 我的数组的输出如下所示:
Array
(
["US"] => 186
["DE"] => 44
["BR"] => 15
["-"] => 7
["FR"] => 3
)
我想用"other"替换"-",所以最后它应该看起来像这样:
Array
(
["US"] => 186
["DE"] => 44
["BR"] => 15
["other"] => 7
["FR"] => 3
)
有人能帮我解决这个问题吗?str_replace对我没用...
如果可以的话,我希望将数组中的"other"部分放在底部 - 就像这样:
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["-"]);
但这对我没起作用 :/
在这个问题中,出现了一个错误消息"Undefined index: -",这是因为数组中没有名为"-"的索引。问题的解决方法是使用unset函数从数组中删除指定的元素。以下是解决方法的代码:
$arr['other'] = $arr['-']; unset($arr['-']);
这段代码的第一行将数组中的"- "元素的值存储在一个名为"other"的新元素中。当以这种方式为具有命名索引的数组创建新元素时,新元素将自动放置在数组的末尾。第二行代码从数组中删除了"- "元素。
解决方法的结果是:
Array ( ["US"] => 186 ["DE"] => 44 ["BR"] => 15 ["FR"] => 3 ["other"] => 7 )