How can I sort multiple arrays in PHP with values being dependent on each other to be in the same position?

Viewed 34

I'm trying to create these array maps of values, but I need them to be sorted in a way were different values are dependent on each other being in the same position. You could view them as distribution maps but within each map they "belong" to other values in other arrays. There is a master array which holds the relations between values.

All arrays are 100% identical in length. It's almost like "a table" where each row is the individual array.

These are the arrays I have (pseudo):

$array1 = [1,2,3,4,5];
$array2 = [6,7,8,9,10];
$array3 = [11,12,13,14,15];

And this is my master list of dependencies (basically how they depend on each other):

$sort = [[1,7,13],[2,6,11],[1,8,15]];

As you can see not all values are present. It's only the values being dependent on each other that are present. How can I now achieve my outcome which is theory would be something like this:

$array1 = [1,2,3,4,5];
$array2 = [7,6,8,9,10];
$array3 = [13,11,15,12,14];

Notice how "8" and "15" doesn't really have anything more to map against so they are just left as combined at the end of their own array. This isn't super important, they could be randomized to just keep the first keys of the array intact for as long as it's possible to create the sorting as intended.

0 Answers
Related