首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP中比较数组并选择最佳匹配

在PHP中比较数组并选择最佳匹配
EN

Stack Overflow用户
提问于 2013-06-14 09:37:08
回答 1查看 54关注 0票数 0

我正在尝试通过ajax比较用户提交的表单,该表单将比较所做的选择与可用的产品,并为所选的给定选项选择最好的一个

产品被组织在一个数组中

代码语言:javascript
复制
$tour = [
 ["id" => "894", "name"=>"Polynesian Cultural Center Super Ambassador Luau",   "greeting"=> true,"canoeRide"=>true,"behindScenes"=>true,"dvd"=>true,"desert"=>true,"tourGuide"=>true,"dinner"=>3, "seatingLevel"=>3,"combo"=>false],    
 ["id" => "897", "name"=>"Polynesian Cultural Center - Ali'i Luau", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>2, "seatingLevel"=>2,"combo"=>false],
 ["id" => "900", "name"=>"Polynesian Cultural Center - Admission and Show", "greeting"=> false,"canoeRide"=>false,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>0, "seatingLevel"=>1,"combo"=>"basic"],
 ["id" => "901", "name"=>"Polynesian Cultural Center - Ambassador Ali'i Luau", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>true,"desert"=>true,"tourGuide"=>true,"dinner"=>3, "seatingLevel"=>2,"combo"=>false],
 ["id" => "805", "name"=>"Pearl Harbor/Dole Plantation/Polynesian Cultural Center", "greeting"=> false,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>2, "seatingLevel"=>2,"combo"=>"pearlDole"],
 ["id" => "931", "name"=>"North Shore and Polynesian Cultural Center With Luau and Show", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>3, "seatingLevel"=>2,"combo"=>"northShore"],
 ["id" => "807", "name"=>"Polynesian Cultural Center/Dole Plantation and North Shore", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>1, "seatingLevel"=>0,"combo"=>"dole"],
 ["id" => "898", "name"=>"Pearl Harbor and Polynesian Cultural Center with Luau and Show", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>2, "seatingLevel"=>2,"combo"=>"pearl"],
 ["id" => "815", "name"=>"Ultimate Hawaii Experience Package", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>true,"tourGuide"=>false,"dinner"=>2, "seatingLevel"=>2,"combo"=>"all"],
 ["id" => "879", "name"=>"Pearl Harbor and Hawaiian Luau Package", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>2, "seatingLevel"=>1,"combo"=>"pearl"],
 ["id" => "1029", "name"=>"3-Day Oahu Sightseeing Experience Package", "greeting"=> true,"canoeRide"=>true,"behindScenes"=>false,"dvd"=>false,"desert"=>false,"tourGuide"=>false,"dinner"=>2, "seatingLevel"=>2,"combo"=>"all"]
 ];

我尝试与这些产品进行比较的对象是:

代码语言:javascript
复制
  {"id":null, "name":"null", "greeting":true,"canoeRide":true,"behindScenes":false,"dvd":true,"desert":true,"tourGuide":false,"dinner":null, "seatingLevel":null,"combo":"dole"};

对象将根据用户输入进行动态更改,这只是一个示例。

进行比较操作的最佳方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-14 09:46:31

您想要与输入有最大共同点的项目吗?

代码语言:javascript
复制
function distance($a, $compare_with) {
    $in_common = 0;
    foreach ($compare_with as $key => $value) {
        $in_common += ($value == $a[$key]);
    }
    return $in_common;
}

function most_in_common($object_list, $compare_with) {
    $in_common = array();
    foreach ($object_list as $key => $val) {
        $in_common[$key] = distance($val, $compare_with);
    }
    return $object_list[array_search(max($in_common), $in_common)];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17099839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档