0

I'm trying to compare a user submitted form via ajax that will compare the selections made against available products, and choose the best one for the given options selected

the products are organized within an array

$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"]
 ];

the object that I am trying to compare against these products is:

  {"id":null, "name":"null", "greeting":true,"canoeRide":true,"behindScenes":false,"dvd":true,"desert":true,"tourGuide":false,"dinner":null, "seatingLevel":null,"combo":"dole"};

the object will be changed dynamically based on user input, this is just an example.

What is the best way to go about the comparison operation?

4

1 回答 1

1

您想要与输入最相似的项目吗?

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)];
}
于 2013-06-14T01:46:31.460 回答