Rearrange words in Array based on position of the first array. In my code there are two array my first array is the base array from which i am going to compare it with second array and make the position same as first array.
Consider 2 input By considering 1 input as base i am applying levenshtein(metaphone(each word database),metaphone(each word of bank)) then based on that arranging the words of bankdata in new array
databaseName = LAL BAHADUR SHASTRI bankdata = SHASTRI LAL source code will only rearrange bankdata and stored in in new array current output of bankdata : LAL SHASTRI
Rearrangement is happening properly just need to arrange words in array
$db = 'LAL BAHADUR SHASTRI YADAV';
$bank = 'SHASTRI LAL';
$a = reArrangeArray($db,$bank);
function reArrangeArray($db,$bank)
{
$dataBaseName = $db;
$bankdataRows = [$db,$bank,];
$dbWords = preg_split("#[\s]+#", $dataBaseName);
foreach ($bankdataRows as $bankdata)
{
$bankWords = preg_split("#[\s]+#", trim($bankdata));
$result = [];
if(!empty($bankWords))
foreach ($dbWords as $dbWord)
{
$idx = null;
$least = PHP_INT_MAX;
foreach ($bankWords as $k => $bankWord)
if (($lv = levenshtein(metaphone($bankWord),metaphone($dbWord))) < $least)
{
$least = $lv;
$idx = $k;
}
@$result[] = $bankWords[$idx];
unset($bankWords[$idx]);
}
$result = array_merge($result, $bankWords);
var_dump($result);
}
}
Case 1: CURRENT OUTPUT
array (size=4)
0 => string 'LAL' (length=3)
1 => string 'BAHADUR' (length=7)
2 => string 'SHASTRI' (length=7)
3 => string 'YADAV' (length=5)
array (size=4)
0 => string 'LAL' (length=3)
1 => string 'SHASTRI' (length=7)
2 => null
3 => null
Expected Output
I need array position as same as databaseArray
$dbName = 'LAL BAHADUR SHASTRI YADAV';
$bankName = 'SHASTRI LAL';
array of db (size=4)
0 => string 'LAL' (length=3)
1 => string 'BAHADUR' (length=7)
2 => string 'SHASTRI' (length=7)
3 => string 'YADAV' (length=5)
array of bankname (size=4)
0 => string 'LAL' (length=3)
1 => #
2 => string 'SHASTRI' (length=7)
3 => ###
if word not found in first array it should be place with # since position is 3 which dont have matching element it has 3 #
Case 2
Input :
$dbName = NikithaRani MohanRao $bankdata = Nikitha Rani Mohan Rao
Output : $newbankdata = NikithaRani MohanRao
It should concatenate the word if found concatenated in $dbName
Expected output attached
![expectedoutput|347x225](upload://ugWEL3bmKfjKQhwTWxbg56rULQJ.png)