开发者

Sort array1 by array2 with SIMILAR key matches and not EXACT key matches

开发者 https://www.devze.com 2023-03-14 05:32 出处:网络
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

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):

  1. $Array1 is the user entered data.
  2. $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
)

$Arr开发者_JAVA百科ay2 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
)


Here's a solutions:

<?php

$array1 = 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 = 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'
);


// Find matching song titles (case insensitive).
$tmp = array_values(array_uintersect($array1, array_flip($array2), 'strcasecmp'));

if ( ! empty($tmp) )
{
  // Generate the array.
  $matches = array_flip(array_uintersect(array_flip($array2), $tmp, 'strcasecmp'));

  print_r($matches);
}
else
  echo 'No matches found.';

?>

This will output:

Array
(
    [Taylor Swift - You Belong With Me] => bbbbbb
    [Sean Kingston - Fire Burning] => cccccc
    [Kelly Clarkson - Gone] => eeeeee
)

The other 2 matches are not 100% identical. As other have suggested, you could use similar_text() or other functions to determine how similar two strings are. If you'd like to do this, you can change the 'strcasecmp' in the array_uintersect call and write your own function that than uses similar_text (or other functions) to decide wether or not the values do in fact intersect or not.


Try using something like the Levenshtein distance or similar_text functions to compare the strings in the arrays. You would just need to determine a threshold that matches as accurately as possible with the least false positives.

0

精彩评论

暂无评论...
验证码 换一张
取 消