I'm trying to compare Advanced Custom Fields data from users with certain post data. Basically I have a user "radio" field (with a single value) where they can select between operating systems (such as Windows, Linux...). Depending on the type, they also can select the specific OS-version (as Windows 10 on a other field). I have also the post field where I can select multiple OS-versions (in my case which are supported by the game). For my example, the fields and their saved values:
- User OS type: "windows"
- User OS version: "10"
- Post multi select os: "array( 'windows-7','mac-os-10.07','linux-ubunto-16.04')"
So now to check if the user OS supports the the post OS - First I would check if the user has generally installt the right os. Which could be done with this code:
$field_os = array('windows-7','mac-os-10.07','linux-ubunto-16.04');
$user_os_type = "windows";
if (strpos($field_os, $user_os_type) !== false) {
echo "It turns true because it found the word 'windows' in the array";
}
But in the second step I also have to check if the users the right os-version. This a litle bit tricky because: In this example the required os version is at least "windows-7", so I would be fine with os version: "10". But you see I have to filter the right os type from the array and than to remove the prefix "windows-" from it. So that I can compare the numbers like that:
$value = '10'; if($value >=7) {
echo "You have the right windows version";
}
Edit:
The question is, how to find this string $user_os_type = "windows"
in a ACF multi select field and store the similar match (which should be in this case "windows-7") in a variable.
Edit:
I'm using this code now because the other one dosen't search as it should. It only worked if 'windows-7' is at the first place.
$input = $user_os_type; // search for this string
$words = $os_array['value']; // word array as source of comparison
$shortest = -1;
foreach ($words as $word) { $lev = levenshtein($input, $word);
if ($lev == 0) { $match = $word; $shortest = 0; break; }
if ($lev <= $shortest || $shortest < 0) {
$match_os = $word; $match_os = preg_replace('/[^0-9\.]/','',$match_os);
$shortest = $lev;
} }