I would like the song titles in $Array2 to be sorted in the same order of $Array1 without losing the values from $Array2. The values from $Array2 should follow the new order of each key in $Array2. I believe the current function I have provided is a solid start...
I have two arrays (please notice the differences in each):
- $Array1 is the user entered data.
- $Array2 is the data looked up on as external source that is SIMILAR to $Array1 but not EXACT.
For example...
$Array1 contains:
Array ( [0] => 3oh!3 - Don't Trust me [1] => Taylor Swift - You Belong with me [2] => Sean Kingston - Fire Burning [3] => Green Day - Know Your Enemy [4] => Kelly Clarkson - Gone )
$Array2 contains:
Array ( [Taylor Swift - You Belong With Me] => bbbbbb [Sean Kingston - Fire Burning] => cccccc [Kelly Clarkson - Gone] => eeeeee [3OH!3- Don't Trust Me lyrics] => aaaaaa [Green Day Know Your Enemy Official] => dddddd )
I already have a function started that I found on this website:
function sortArrayByArray(array $toSort, array $sortByValuesAsKeys)
{
$commonKeysInOrder = array_intersect_key(array_flip($sortByValuesAsKeys), $toSort);
$commonKeysWithValue = array_intersect_key($toSort, $commonKeysInOrder);
$sorted = array_merge($commonKeysInOrder, $commonKeysWithValue);
return $sorted;
}
However...
$sortArray = sortArrayByArray($Array2, $Array1);
print_r($sortArray);
$sortArray
is only returning two results:
Array
(
[Sean Kingston - Fire Burning] => cccccc
[Kelly Clarkson - Gone] => eeeeee
)