Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从匹配的两个数组中获取值数组?_Php_Arrays - Fatal编程技术网

Php 如何从匹配的两个数组中获取值数组?

Php 如何从匹配的两个数组中获取值数组?,php,arrays,Php,Arrays,我有两个数组,然后尝试通过匹配这两个数组来获取值数组。 不可能?让我们看看我的代码 $text = array( "I had locked the door before I left my home", "I was late. The train had left",

我有两个数组,然后尝试通过匹配这两个数组来获取值数组。 不可能?让我们看看我的代码

$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"     
//    ); 

我该怎么做?谢谢:)

您可以使用
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);
结果

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

您可以使用
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);
结果

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

我已经完成了一个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);
$finaloutput=array();
$text=数组(
“我离家前就锁好了门”,
“我迟到了,火车已经开走了”,
“在我破门之前,她已经找到了钥匙”,
“不……”
);
$inline=数组(“锁定”、“找到”);
foreach($inline作为$find){
$input=preg_quote($find,“~”);
$result=preg_grep(“~”.$input.“~”,$text);
$finaloutput[]=$result;
}
$finalArray=call\u user\u func\u数组('array\u merge',$finaloutput);
回声“;打印费($finalArray);
即使您使用Locked或Loc或lo进行搜索,它也会获取您的数据。
希望有帮助

我已经使用了一个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);
$finaloutput=array();
$text=数组(
“我离家前就锁好了门”,
“我迟到了,火车已经开走了”,
“在我破门之前,她已经找到了钥匙”,
“不……”
);
$inline=数组(“锁定”、“找到”);
foreach($inline作为$find){
$input=preg_quote($find,“~”);
$result=preg_grep(“~”.$input.“~”,$text);
$finaloutput[]=$result;
}
$finalArray=call\u user\u func\u数组('array\u merge',$finaloutput);
回声“;打印费($finalArray);
即使您使用Locked或Loc或lo进行搜索,它也会获取您的数据。 希望有帮助

您可以使用函数

你可以使用这个函数


不是专家,但我喜欢利用自己。不确定使用它而不是STRPO是否会影响性能。但是,能够在模式之间使用OR运算符,就可以轻松地将模式内爆为字符串,并一次搜索所有模式,而不必进行另一个循环

foreach( $texts as $key => $text )
{
    if( preg_match( '#\b\Q' . implode( '\E\b|\b\Q', $patterns ) . '\E\b#', $text )) {
        $output[$key] = $text;
    }
}
以下是根据@mickmackusa建议进行的修改,如果您不希望模式中的任何字符被误解为正则表达式控制字符(例如:一个充当通配符的句点),并且您希望确保要查找的模式字是单个字,则需要进行此修改(不是另一个词的一部分)


我不是专家,但我喜欢自己使用。不确定使用它而不是strop是否会对性能造成影响。但是,能够在模式之间使用OR运算符可以轻松地将模式内爆为字符串并一次搜索所有模式,而不必进行另一个循环

foreach( $texts as $key => $text )
{
    if( preg_match( '#\b\Q' . implode( '\E\b|\b\Q', $patterns ) . '\E\b#', $text )) {
        $output[$key] = $text;
    }
}
以下是根据@mickmackusa建议进行的修改,如果您不希望模式中的任何字符被误解为正则表达式控制字符(例如:一个充当通配符的句点),并且您希望确保要查找的模式字是单个字,则需要进行此修改(不是另一个词的一部分)

preg_grep()
非常适合通过一个函数调用过滤数组元素。只需添加
单词边界
,并使用(
\Q..\E
)生成字符串
literal

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

代码:()

输出:

$pattern='/\b\Q'.implode('\E\b|\b\Q',$inline).'\E\b/i';
有关
\Q
\E
的更多阅读信息,请参阅:

有关
\b
的更多信息,请参阅:

有关
|
(替代)的更多信息,请参阅:

p、 s.如果需要不区分大小写的匹配,请使用
i
pattern修饰符:

preg_grep()
非常适合通过一个函数调用过滤数组元素。只需添加
单词边界
,并使用(
\Q..\E
)生成字符串
literal

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

代码:()

输出:

$pattern='/\b\Q'.implode('\E\b|\b\Q',$inline).'\E\b/i';
有关
\Q
\E
的更多阅读信息,请参阅:

有关
\b
的更多信息,请参阅:

有关
|
(替代)的更多信息,请参阅:

p、 s.如果需要不区分大小写的匹配,请使用
i
pattern修饰符:


看了?还是锁了?选择..输入错误先生,我是说锁了:)你是否也想要一个字符串,如鸟聚在一起。也一样?还是只想匹配整个单词?在为你的项目选择最佳方法时,这是一个关键的决定。是的,当然,就像鸟聚在一起。。嗯,你在这里看到的最佳答案是什么?因为几乎所有东西都有相同的结果。我是伊索尔廷g过度匹配的问题。如果您的搜索字符串作为较大单词的子字符串存在,则可能会保留您不想要的结果。是否希望以区分大小写的方式进行匹配?这些决定决定决定是否适合您的任务。您的问题越清楚,您的答案看起来就越好?