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

时间:2015-02-05 10:29:46

标签: php arrays

我有这个数组样本:

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年 - 产品编号 260 - 这是可变的,它是未知的 电话 - 是产品制造 诺基亚是产品型号 众所周知,排除变量..

我如何搜索数组,如果我有:

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

被认可为不同的产品 我希望你能理解我的意思。直到现在我使用了in_array,但是在最近在字符串中添加变量之后,这不起作用.. 谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

在尝试实现您未使用explode()函数调用的愿望时,我创建了两个可以使用型号和制造商匹配的函数。第一个函数使用正则表达式匹配,而第二个函数使用字符串操作。性能明智我不知道。它们可以适当地优化A LOT,但功能应该在那里。

正则表达式版本的优势在于它可以捕获未知数字。如果您不想要这个功能,可以很容易地重构。但我的猜测是它有点重要。如果没有,那么为什么要把它存放在第一位: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)

答案 1 :(得分:0)

$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);
相关问题