0

i have a array - the output of my array looks like this:

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

and i want to replace the "-" with "other"

so it should look like this at the end:

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

could someone help me with this? str_replace havent worked with me... and if you could i want to have the "other" part of the array at the bottom - like this:

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

thanks :)

current code:

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

but this havent worked for me :/

PHPprogrammer42
  • 355
  • 1
  • 3
  • 7

2 Answers2

2

Simple like that:

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

At the end, the array will be like this:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["FR"] => 3
    ["other"] => 7
)
Vitor Villar
  • 1,855
  • 18
  • 35
1
$arr['other'] = $arr['-'];
unset($arr['-']);

The first command stores the value of your $arr['-'] element in a new element named $arr['other']. When you create a new element this way for an array with named indexes the new element will automatically be placed at the end of the array.

The second command removes the $arr['-'] element from the array. The result will be:

Array
(
    ["US"] => 186
    ["DE"] => 44
    ["BR"] => 15
    ["FR"] => 3
    ["other"] => 7
)
RST
  • 3,899
  • 2
  • 20
  • 33