# How to sort multidimensional array by values of another array nemnogonado?

It is necessary that the second array has been sorted by values of the second
Another problem is that the second array can be the key more than the first, and they have to go further in the order

1 array
``````Array
(
[1] => B SPB
[2] => Z SPZ
[3] => A SPA
[4] => C SPC
)``````

2 array
``````Array
(
[A SPA] => Array
(
[title] => A SPA
[resources] => Array
(
[208] => 208
)
)

[B SPB] => Array
(
[title] => B SPB
[resources] => Array
(
[232] => 232
)
)

[C SPC] => Array
(
[title] => C SPC
[resources] => Array
(
[257] => 257
)
)

[HTD] => Array
(
[title] => HTD
[resources] => Array
(
[277] => 277
)
)

[Z SPZ] => Array
(
[title] => Z SPZ
[resources] => Array
(
[188] => 188
)
)
)``````
March 19th 20 at 09:25
March 19th 20 at 09:27
Solution
You need to go through the first array, get the value from the second, add to the result array, and make unset this value in the second. After iterating through all values of the first array, the result of merge (array_merge) that remained in the second array

``````\$resultArray = [];
foreach (\$firstArray as \$key) {
if (isset(\$secondArray[\$key])) {
\$resultArray[\$key] = \$secondArray[\$key];
unset(\$secondArray[\$key]);
}
}

\$resultArray = array_merge(\$resultArray, \$secondArray);``````

UPDATE checked and corrected
sandbox.onlinephpfunctions.com/code/cc9e81e851d84c...
I wanted to upload fixed code, and you have made it yourself!
Thank you! - Malachi_Willms commented on March 19th 20 at 09:30

Find more questions by tags ArraysPHP