3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function prepareArray($str) { $onlyLowerLetters = preg_replace('/[^a-z]/', '', strtolower($str)); $stringArray = str_split(str_replace(' ', '', $onlyLowerLetters)); $uniqueArray = array_unique($stringArray); $countsArray = []; foreach ($uniqueArray as $letter) { $countsArray[$letter] = substr_count($onlyLowerLetters, $letter); } unset($letter); foreach ($countsArray as $k => $v) { if ($v === 1) unset($countsArray[$k]); } ksort($countsArray); reset($countsArray); return $countsArray; } function getIntersect($a1, $a2) { $ar = array_intersect($a1, $a2); $intersect = []; foreach ($ar as $k => $v) { $intersect[$k] = $v; } return $intersect; } function compareArrays($a1, $a2) { // var_export($a1); // echo "\n"; // var_export($a2); // echo "\n"; $resultArray = []; foreach ($a1 as $k => $v) { $resultArray[$k][0] = $v; if (isset($a2[$k]) && $a1[$k] > $a2[$k]) { $resultArray[$k][0] = $v; } } foreach ($a2 as $k => $v) { $resultArray[$k][1] = $v; if (isset($a1[$k]) && $a1[$k] < $a2[$k]) { $resultArray[$k][1] = $v; } } return arsort($resultArray); } function mix($s1, $s2) { $s1Prepared = prepareArray($s1); $s2Prepared = prepareArray($s2); var_export($s1Prepared); echo "\n"; var_export($s2Prepared); echo "\n"; $intersectArray = getIntersect($s1Prepared, $s2Prepared); // var_export($intersectArray); // echo "\n"; $diffArray = compareArrays(array_diff_key($s1Prepared, $intersectArray), array_diff_key($s2Prepared, $intersectArray)); var_export($diffArray); echo "\n"; // $allLetters = array_merge($diffArray, $intersectArray); // var_export($allLetters); } mix("Are they here", "yes, they are here");
based on U97Yf
Output for 5.6.0 - 7.3.0beta1
array ( 'e' => 4, 'h' => 2, 'r' => 2, ) array ( 'e' => 5, 'h' => 2, 'r' => 2, 'y' => 2, ) true