如何通过匹配两个数组获取值数组?

时间:2017-12-20 04:44:48

标签: php arrays

我有两个数组,然后尝试从匹配两者中获取值数组。 这不可能吗?让我们检查一下我的代码

$text = array(                                                               
              "I had locked the door before I left my home",                 
              "I was late. The train had left",                            
              "She had already found the key, before I broke the door",  
              "n..."                                                         
         );

$inline =  array("locked","found");

$output = ?                        
print_r($output);                                 

// i want a $output like this                                                       
// array(                                                                  
//      [0] => "I had locked the door before I left my home"               
//      [2] => "She had already found the key, before I broke the door"     
//    ); 

我该怎么做?谢谢:))

5 个答案:

答案 0 :(得分:2)

您可以使用array_filter()功能

$output = array_filter($text, function($e) use($inline){
    foreach ($inline as $search) {
        if (strpos($e, $search) > -1) return true;
    }
    return false;
});

print_r($output);

答案 1 :(得分:1)

您可以使用strpos并嵌套foreach

$text = array(                                                               
              "I had locked the door before I left my home",                 
              "I was late. The train had left",                            
              "She had already found the key, before I broke the door",  
              "n..."                                                         
         );

$inline =  array("locked","found");
$output = array();
//Loop our sentences
foreach($text as $key => $sentence){
    //Check the word
    foreach($inline as $find){
        //Check if the word is in sentence
        if(strpos($sentence,$find) !== false){
            $output[$key] = $sentence;
        }
    }
}
print_r($output);

<强> RESULT

Array
(
    [0] => I had locked the door before I left my home
    [2] => She had already found the key, before I broke the door
)

答案 2 :(得分:1)

我已经完成了一个foreach,我使用了preg_quote和preg_grep: -

$finaloutput = array();
$text = array(                                                               
          "I had locked the door before I left my home",                 
          "I was late. The train had left",                            
          "She had already found the key, before I broke the door",  
          "n..."                                                         
     );
$inline =  array("locked","found");
foreach($inline as $find){
    $input = preg_quote($find, '~');
    $result = preg_grep('~' . $input . '~', $text); 
    $finaloutput[] = $result;
}
$finalArray = call_user_func_array('array_merge', $finaloutput);
echo "<pre>"; print_r($finalArray);

即使您使用Locked或Loc进行分离,也可以获取数据。 希望它有所帮助!

答案 3 :(得分:0)

不是专家,但我喜欢自己使用preg_match。不确定使用它而不是strpos是否有性能损失。但是,能够在模式之间使用OR运算符可以很容易地将模式内嵌到字符串中并立即搜索所有模式,而不必进行另一个循环。

$texts = [
    "I had locked the door before I left my home",
    "I was late. The train had left",
    "She had already found the key, before I broke the door",
    "n..."
];
$patterns = [ "locked", "found" ];
$output = [];

foreach( $texts as $text )
{
    if( preg_match( '#' . implode( '|', $patterns ) . '#', $text )) {
        $output[] = $text;
    }
}

print_r( $output );

根据@mickmackusa建议,这是一个修改,在你不希望模式中的任何字符被误解为正则表达式控制字符(例如:充当通配符的句点)并且如果你我想确保你要找的模式单词是单个单词(不是另一个单词的一部分)。

foreach( $texts as $key => $text )
{
    if( preg_match( '#\b\Q' . implode( '\E\b|\b\Q', $patterns ) . '\E\b#', $text )) {
        $output[$key] = $text;
    }
}

答案 4 :(得分:0)

preg_grep()非常适合使用一个函数调用来过滤数组元素。唯一需要做的准备就是添加word boundaries并将字符串literal与(\Q..\E)匹配。

事实上,您的样本$inline数据不需要\Q\E,但这只是一种预防措施。同样,样本输入数据不需要\b,但这是将来验证脚本的最佳实践。

代码:(Demo

$text = array(                                                               
    "I had locked the door before I left my home",                 
    "I was late. The train had left",                            
    "She had already found the key, before I broke the door",  
    "n..."                                                         
);

$inline =  array("locked","found");
$pattern='/\b\Q'.implode('\E\b|\b\Q',$inline).'\E\b/';  // renders as: /\b\Qlocked\E\b|\b\Qfound\E\b/
var_export(preg_grep($pattern,$text));

输出:

array (
  0 => 'I had locked the door before I left my home',
  2 => 'She had already found the key, before I broke the door',
)

有关\Q\E的更多阅读,请参阅:https://www.regular-expressions.info/characters.html

的特殊字符部分

有关\b的更多信息,请参阅:https://www.regular-expressions.info/wordboundaries.html

有关|(轮换)的更多信息,请参阅:https://www.regular-expressions.info/alternation.html

P.S。如果您需要不区分大小写的匹配,请使用i模式修饰符:

$pattern='/\b\Q'.implode('\E\b|\b\Q',$inline).'\E\b/i';