You can enjoy/abuse the effect that array_flip()
has on an array containing duplicate values -- it will overwrite earlier encountered values with later encountered values.
After that, you can use three calls of array_diff_key()
or just loop over the flipped data and access the data based on the remaining indexes.
Code: (Demo)
$new1 = [];
$new2 = [];
$new3 = [];
foreach (array_flip($firstArray) as $index) {
$new1[] = $firstArray[$index];
$new2[] = $secondArray[$index];
$new3[] = $thirdArray[$index];
}
var_dump($new1, $new2, $new3);
Or you could push references into the result arrays and keep overwriting the mapped values if they are re-encountered. (Demo)
$new1 = [];
$new2 = [];
$new3 = [];
foreach ($firstArray as $index => $value) {
if (!isset($ref[$value])) {
$new1[] = &$ref[$value][0];
$new2[] = &$ref[$value][1];
$new3[] = &$ref[$value][2];
}
$ref[$value][0] = $value;
$ref[$value][1] = $secondArray[$index];
$ref[$value][2] = $thirdArray[$index];
}
var_dump($new1, $new2, $new3);