我有以下字符串数组
$array = array(
"key" => "zhcdde,hzccd,eezhg"
);
我希望得到这些值,然后将它们与','分开。并计算相等的字母。仅举例来说" z"和" h"是相同的,因为这些是每个字符串组合中出现的唯一字符。我尝试了一些这样的事情
$equalLetters = 0;
foreach ($array as $key => $splitWords) {
$words = explode(",", $splitWords);
for ($letter = 0; $letter < strlen($words[0]); $letter++) {
if ($words[0][$letter] == $words[1][$letter] || $words[0][$letter] == $words[2][$letter] || $words[1][$letter] == $words[2][$letter]) {
$equalLetters++;
}
}
}
echo $equalLetters;
没有成功。你能帮助我吗?
答案 0 :(得分:3)
我不清楚关联数组是如何影响它的,但是在这里你可以如何获得字符串中的常用字母:
<?php
$string = 'zhcdde,hzccd,eezhg';
$words = explode(',', $string);
$letters = array_map('str_split', $words);
$common = call_user_func_array('array_intersect', $letters);
var_dump($common);
?>
哪个输出:
array(2) {
[0]=>
string(1) "z"
[1]=>
string(1) "h"
}