我正在尝试通过ajax比较用户提交的表单,该表单将比较所做的选择与可用的产品,并为所选的给定选项选择最好的一个
产品被组织在一个数组中
$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"]
];我尝试与这些产品进行比较的对象是:
{"id":null, "name":"null", "greeting":true,"canoeRide":true,"behindScenes":false,"dvd":true,"desert":true,"tourGuide":false,"dinner":null, "seatingLevel":null,"combo":"dole"};对象将根据用户输入进行动态更改,这只是一个示例。
进行比较操作的最佳方法是什么?
发布于 2013-06-14 09:46:31
您想要与输入有最大共同点的项目吗?
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)];
}https://stackoverflow.com/questions/17099839
复制相似问题