G
G
GoSou2018-10-04 01:03:36
Arrays
GoSou, 2018-10-04 01:03:36

How to rename keys in an array?

Hello everyone
, I have an array like this:

Array
(
    [0] => красный
    [1] => синий
    [4] => зелёный
)

I need to rename the keys so that the array looks like this:
Array
(
    [0] => red
    [1] => blue
    [4] => green
)

In other words, change the names of colors from Russian to English. How to do it?
I read a lot of articles on the Internet, but, apparently, there is already “porridge” in my head and in practice nothing really works. Please tell me the right way to solve this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2018-10-04
@GoSou

$translations = ['красный' => 'red', 'синий' => 'blue', 'зелёный' => 'green'];
$input = [0 => 'красный', 1 => 'синий', 4 => 'зелёный'];
foreach ($input as $k => $v) {
    isset($translations[$v]) && $input[$k] = $translations[$v];
}
print_r($input);
/*
Array
(
    [0] => red
    [1] => blue
    [4] => green
)
*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question