Good day,
So I have the following array to start with that has items added to it. The order the items are added in the list have no specific sort to them to begin with other than the order they were originally added to the array in. For ease of reading, I put them in descending order to explain the result I am looking for. The values share a sub key of "tid" for this example.
Array
(
[0] => Array
(
[tid] => value_5
)
[1] => Array
(
[tid] => value_4
)
[2] => Array
(
[tid] => value_3
)
[3] => Array
(
[tid] => value_2
)
[4] => Array
(
[tid] => value_1
)
)
I now want to be able to feed a second array into a function, and have the array resorted so that the values supplied, which correlates to the "tid" of the field, are resorted to the front of the array in the order which they are given, and then leave the rest of the array in the order it currently is. For example, if I pass the following array as a sort key:
array("value_2", "value_3");
Then the value_2
and value_3
rows should become array key 0 and 1 respectively, but the rest of the array should stay in the same order like the following
Array
(
[0] => Array
(
[tid] => value_2
)
[1] => Array
(
[tid] => value_3
)
[2] => Array
(
[tid] => value_5
)
[3] => Array
(
[tid] => value_4
)
[4] => Array
(
[tid] => value_1
)
)
Alternatively, if a value that doesn't exist like value_9
is passed in the second array, we would just want to ignore that and skip past it.
I have explored a few options like a foreach
and for
loop and compared the values but its slow and doesn't really have recursion which seems like may be needed on this function.
As array sorting is a hot topic in PHP due to the myriad of ways to do so, I am looking to you guys for the best way to handle this problem. Thanks!