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 :/

1
  • damn.. need to go offline now.. but i will response to anyone who tries to help me :) Commented Jan 4, 2016 at 14:58

2 Answers 2

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
)
Sign up to request clarification or add additional context in comments.

11 Comments

i edited my question :) this in your answer havent worked for me :/
php just gives me an: Undefined index: - ( so php doesnt find the "-" character )
Are you sure the array $count_land have the "-" index? If you print_r the $count_land what will be the output?
what you see in my question is the output of $count_land :)
yes i got it :) without var dump i havent seen that there were more characters as i thought :p now it works perfekt :) thanks a lot :)
|
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
)

3 Comments

You need to explain your answer. SO doesn't exist to just answer questions but teach people how to code.
hello, sorry that i havent replied earlyer... thanks for the answer but it didnt worked with me :o i updated my question so may we can fight a solution :)
php just gave me an: Undefined index: - ( so php doesnt find the "-" character )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.