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; 
} }
  • Welcome to WPSE. You should reword your post so there is an actual question here. You have provided lots of information but this is a Q&A format site. Ask a single, clear question if you want a good, useful answer. – jdm2112 Oct 26 '16 at 20:34
up vote 0 down vote accepted

There is a PHP function: preg_grep which accepts $pattern and $input as a parameters and returns you an array of matches. If you want to get first match just assign it to variable, e.x.:

$field_os = array('windows-7','mac-os-10.07','linux-ubunto-16.04');
$user_os_type = "windows";

$matches = preg_grep("/" . $user_os_type . "/", $field_os);

var_dump($matches);
/*
 Prints:

 array(1) {
  [0]=>
  string(9) "windows-7"
 }

*/

$first_match = $matches[0];

echo $first_match; // Prints: windows-7

For more information visit this link: http://php.net/manual/en/function.preg-grep.php

  • Ok this works, if word array is structured like this $field_os = array('windows-7','mac-os-10.07','linux-ubunto-16.04');. Now I have problems to get AFC multi select field in this format. I've tried to do it like this: $os_array = get_sub_field_object('game_requirements_os'); ob_start(); foreach($os_array['value'] as $value): echo "'" .$value. "',"; endforeach; $field_os_words = ob_get_clean(); but the $first_match does echo the whole string now. – Game Unity Oct 27 '16 at 11:46
  • I figured it out by my self: $field_os = $os_array['value']; does return the values. :) – Game Unity Oct 27 '16 at 14:03
  • Nice to know that :) I'm glad that I put you on the right way – Sebastian Kaczmarek Oct 27 '16 at 14:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.