PHP中的简单用户匹配逻辑[关闭]

I have an html page that has 1 textbox field for username and 5 checkboxes for that user to indicate which genres of music they like (ambient, rock, rap, jazz, pop). I duplicated the form fields twice more so that I can get the following data values:

Tom
Ambient
Rock
Rap

Sue
Rock
Rap
Jazz

Betty
Rock
Rap
Jazz

What is the best way to do a user matching algorithm in PHP? From the values about Sue and Betty would match 100% and Tom would match less than 100% with the other two.

Any suggestions?

below is the code

<p>Put in your name and pick the music genres you like:</p>
<form id="form1" name="form1" method="post" action="">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="12%"><label for="name"></label>
      <input type="text" name="name1" id="name1" vale="name1" placeholder="name 1"/></td>
      <td width="12%"><label for="name"></label>
        <input type="text" name="name2" id="name2" value="name2" placeholder="name 2"/></td>
      <td width="13%"><label for="name3"></label>
        <input type="text" name="name3" id="name3" value="name3" placeholder="name 3"/></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="checkbox" id="checkbox" />
      <label for="checkbox">Ambient</label></td>
      <td><input type="checkbox" name="checkbox" id="checkbox" />
        <label for="checkbox">Ambient</label></td>
      <td><input type="checkbox" name="checkbox" id="checkbox" />
        <label for="checkbox">Ambient</label></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Rock" id="Rock" value="Rock"/>Rock</td>
      <td><input type="checkbox" name="Rock" id="Rock" value="Rock"/>
        Rock</td>
      <td><input type="checkbox" name="Rock" id="Rock" value="Rock"/>
        Rock</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Rap" id="Rap" value="Rap"/>Rap</td>
      <td><input type="checkbox" name="Rap" id="Rap" value="Rap"/>
        Rap</td>
      <td><input type="checkbox" name="Rap" id="Rap" value="Rap"/>
        Rap</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Jazz" id="JAzz" value="Jazz" />Jazz</td>
      <td><input type="checkbox" name="Jazz" id="JAzz" value="Jazz" />
        Jazz</td>
      <td><input type="checkbox" name="Jazz" id="JAzz" value="Jazz" />
        Jazz</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Pop" id="Pop" value="Pop" />Pop</td>
      <td><input type="checkbox" name="Pop" id="Pop" value="Pop" />
        Pop</td>
      <td><input type="checkbox" name="Pop" id="Pop" value="Pop" />
        Pop</td>
    </tr>
  </table>

  <br />
  <input type="submit" name="button" id="button" value="Submit" />
</form>
doudundian9558
doudundian9558 感谢@CodeGodie提示。如果它让人困惑,我改变它来标题中的逻辑。
大约 5 年之前 回复
dty63504
dty63504 你应该自己完成家庭作业。
大约 5 年之前 回复
doukao8851
doukao8851 你能详细解释一下“用户匹配算法”的含义吗?此外,不是列出您的项目,请尝试添加您的HTML代码,否则Stackoverflow将删除您的原始帖子。
大约 5 年之前 回复

3个回答

If you have the options as arrays you could do something with the array_diff function. i.e.

<?php
$tom = array("Ambient", "Rock", "Rap");
$sue = array("Rock", "Rap", "Jazz");
$betty = array("Rock", "Rap", "Jazz");

//compare tom to sue
$total = count($tom);
$diff = count(array_diff($tom, $betty));
$percent = 100 - ($diff / $total * 100);

echo round($percent, 2)."% match between Tom and Sue";



使用关联数组向每个用户添加流派列表。 然后比较循环中的流派:</ p>

  $ user_genre ['Tom'] = array('Ambient','Rock','Rap'); 
$ user_genre ['Sue '] = array('Rock','Rap','Jazz');
$ user_genre ['Betty'] = array('Rap','Rock','Jazz');
$ match = 0;

Nachach($ user_genre ['Tom'] as $ tom_genre){
foreach($ user_genre ['Sue'] as $ sue_genre){
if($ tom_genre == $ sue_genre){
$ match ++;
}
}
}
</ code> </ pre>

不是一个好的代码,只是你应该考虑的方式。</ p>
</ div>

展开原文

原文

Use an associative array to add list of genres to each of users. Then compare genres in loop:

$user_genre['Tom']=array('Ambient', 'Rock', 'Rap');
$user_genre['Sue']=array('Rock', 'Rap', 'Jazz');
$user_genre['Betty'] =array('Rap', 'Rock', 'Jazz');
$match=0;
foreach ($user_genre['Tom'] as $tom_genre){
    foreach ($user_genre['Sue'] as $sue_genre){
        if ($tom_genre == $sue_genre){
            $match++;
        }
    }
}

Not a good code, just the way how you should think about.

Assuming, that you store the user's choices into arrays you could use array_intersect

here is an example:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

The output:#

Array 
(
   [a] => green
   [0] => red 
)

And here is the php manual with all array functions.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐