# How to sort array keys by keys of another array?

There is an array:
``````[
'key2' => \$value2,
'key1' => \$value1,
'key3' => \$value3
];``````

There is a second array that stores their values, the keys of the first in the order we need:
`['key1', 'key2', 'key3'];`
Question: how to sort the first array by the array keys sorter?
Understand that you need to use uksort or foreach?
April 4th 20 at 13:12
April 4th 20 at 13:14
Solution
I understand that two arrays have the same number of values with the same name?
Then everything is simple:
``````// The first array
\$ArrayOne['a'] = 123; \$ArrayOne['b'] = 255; \$ArrayOne['c'] = 378;
// Second array
\$ArrayTwo['b'] = 276; \$ArrayTwo['c'] = 342; \$ArrayTwo['a'] = 198;
// Create a new array
\$ArrayThree = array();
// We put everything into a new array
foreach(\$ArrayTwo as \$key => \$value){\$ArrayThree[\$key] = \$ArrayOne[\$key];}
// Ready array \$ArrayThree it as you need
print_r(\$ArrayThree);
/*
Array
(
[b] => 255
[c] => 378
[a] => 123
)*/``````
April 4th 20 at 13:16
`array_merge(array_flip(\$keys), \$array)`

Find more questions by tags PHP