搜索数组中包含变量的字符串的最快方法


Fastest way to search array for a string that contains variable

我有这个数组示例:

array(3) { 
[0]=> string(20) "1845-260-phone-nokia"
[1]=> string(22) "1133-0-phone-motorola" 
[2]=> string(20) "1133-0-phone-samsung" 
}

我的目标是搜索数组并查找字符串是否存在于数组中。

让我首先解释一下这个字符串的含义:

"1845-260-phone-nokia"

1845-产品id260-这是可变的,未知手机是产品制造诺基亚是产品型号除了变量外,一切都是已知的。。

如果我有,我如何搜索阵列

[0]=> string(20) "18451-260-phone-nokia"

被视为不同的产品我希望你不明白我的意思。到目前为止,我使用了in_array,但最近在字符串中添加了变量后,这就不起作用了。。谢谢你帮我!

在尝试实现不使用explode()函数调用的愿望时,我创建了两个可以使用型号和制造商匹配的函数。第一个函数使用正则表达式进行匹配,而第二个函数使用字符串操作。就表现而言,我不知道。它们都可以适当地进行大量优化,但功能应该存在。

正则表达式版本的优点是它可以捕获未知的数字。如果您不想要这种功能,可以很容易地将其重构掉。但我的猜测是,它在某种程度上很重要。如果不是,那么为什么首先存储它:D

/**
 * Finds the first matching phone using a regular expression.
 *
 * @param array  $phones        An array of available phones
 * @param string $model         The model number of the phone
 * @param string $manufacturer  The manufacturer of the phone
 *
 * @return array|bool Returns an associative array with matched phone and the
 *                    unknown number on success. Returns FALSE if no match.
 */
function find_phone_regex(array $phones, $model, $manufacturer) {
    /*
     * OPS: I have added the 'i' flag to make it case-insensitive.
     * This might not be the wished behavior.
     */
    $regex = '~^' . $model . '-(?<number>[0-9]+)-phone-' . $manufacturer . '$~i';
    foreach($phones as $phone) {
        if(preg_match($regex, $phone, $matches)) {
            return [
                'phone'  => $phone,
                'number' => $matches['number']
            ];
        }
    }
    return false;
}
/**
 * Finds the first matching phone.
 *
 * @param array  $phones        An array of available phones
 * @param string $model         The model number of the phone
 * @param string $manufacturer  The manufacturer of the phone
 *
 * @return string|bool Returns the phone on success. Returns FALSE if it does not exist.
 */
function find_phone_string(array $phones, $model, $manufacturer) {
    $input_model_pos        = strlen($model);
    $input_manufacturer_pos = strlen($manufacturer);
    $model        = strtolower($model);
    $manufacturer = strtolower($manufacturer);
    foreach($phones as $phone) {
        $phone_model        = substr($phone, 0, $input_model_pos);
        $phone_manufacturer = substr($phone, -$input_manufacturer_pos);
        if(strtolower($phone_model) == $model && strtolower($phone_manufacturer) == $manufacturer) {
            return $phone;
        }
    }
    return false;
}

关于参数列表,它们的用法是相同的。只有函数名称不同。使用您提供的数据,两个函数将返回以下内容(用var_dump()显示)。

正则表达式版本。

$phone = find_phone_regex($phones, '1845', 'nokia');
array (size=2)
  'phone' => string '1845-260-phone-nokia' (length=20)
  'number' => string '260' (length=3)

字符串版本。

$phone = find_phone_string($phones, '1845', 'nokia');
string '1845-260-phone-nokia' (length=20)
$array = array("1845-260-phone-nokia", "1133-0-phone-motorola", "1133-0-phone-samsung");
$search = 'nokia';
$results found = array();
foreach ($array as $key => $value){
    if (strpos($value, $search) !== false)
     $results_found[] = $value; 
}
echo 'following results found: '.implode('<br>', $results_found);