3

How can I check if the start of a given value matches an entry in a predefined list of values?

$model_no1 = "KK71458";
$model_no2 = "IX41";

$models = array("KK61", "KK71", "KK81", "IX", "IJ");

In the above sample code, both values should return a valid match.

3
  • Whats your input (x number of strings, one string)? How should your output look? Commented Feb 12, 2014 at 9:57
  • How large arrays and strings do you want to be able to process? Naive solutions to this would be very inefficient for large arrays/strings. Commented Feb 12, 2014 at 9:57
  • Just one string. The array will contain no more than a hundred entries similar to the above. Commented Feb 12, 2014 at 9:58

4 Answers 4

2

Try this:

$model_no1 = "KK71458";
    $models = array("KK61", "KK71", "KK81", "IX", "IJ");
    foreach($models as $key=> $mod){
        if($mod == substr($model_no1,0,strlen($mod))){ 
            echo "key: ".$key.' with value: '.$mod;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend use strpos() function rather than combination of substr() and strlen() in your case:

$model_no1 = 'KK71458';
$model_no2 = 'IX41';

$models = array('KK61', 'KK71', 'KK81', 'IX', 'IJ');
$result1 = check_model($model_no1, $models);
$result2 = check_model($model_no2, $models);

function check_model($model_no, array $models) {
    foreach ($models as $needle) {
        if (0 === strpos($model_no, $needle))
            return true;
    }

    return false;
}

I wrote the simple test for checking string comparison performance by strpos(), strstr() and substr()+strlen(). Here is results:

Test name       Repeats         Result          Performance     
strpos          10000           0.167221 sec    +0.00%
strstr          10000           0.169299 sec    -1.24%
substr+strlen   10000           0.207363 sec    -24.01%

As you see strpos() has best performance results.

Comments

0
$models = array("KK61", "KK71", "KK81", "IX", "IJ");

$g = function($search) use ($models) {
    return array_filter($models, function($string) use($search) {
        return substr($search, 0, strlen($string)) == $string;
    });
};

print_r($g->__invoke('KK71458'));
print_r($g->__invoke('IX41'));
print_r($g->__invoke('SPONGEBOB'));

Comments

0
HarryIsBack has reversed $mod and $model_no1.

Correct like this :

$model_no1 = "KK71458";
    $models = array("KK61", "KK71", "KK81", "IX", "IJ");
    foreach($models as $key=> $mod){
        if($model_no1 == substr($mod,0,strlen($model_no1))){ 
            echo "key: ".$key.' with value: '.$mod;
        }
    }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.