Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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,这一个有点棘手,我有一个数组,我只需要在其中保留某些值字符串 $getpositions = file("index.php"); $searchpoz = array('NEED1', 'NEED2', 'WANT THIS ALSO','ANDTHIS'); function strposa($haystack, $needles=array(), $offset=0) { $chr = array(); foreach($needles as $needl

这一个有点棘手,我有一个数组,我只需要在其中保留某些值字符串

$getpositions = file("index.php");
$searchpoz = array('NEED1', 'NEED2', 'WANT THIS ALSO','ANDTHIS');

function strposa($haystack, $needles=array(), $offset=0) {
        $chr = array();
        foreach($needles as $needle) {
                $res = strpos($haystack, $needle, $offset);
                if ($res !== false) $chr[$needle] = $res;
        }
        if(empty($chr)) return false;
        return min($chr);
}//http://stackoverflow.com/a/9220624/594423


foreach($getpositions as $key => $clearlines) {
    if(strposa($clearlines, $searchpoz) == false)
        unset($getpositions[$key]);
}
$positionsorder = array_values($getpositions);
print_r($positionsorder);

Array
(
    [0] =>      i dont need this NEED1 i dont need this

    [1] =>      i dont need this NEED2 i dont need this

    [2] =>      i dont need this WANT THIS ALSO i dont need this

    [3] =>      i dont need this ANDTHIS i dont need this

)
所以期望的输出应该是

Array
(
    [0] =>NEED1

    [1] =>NEED2

    [2] =>WANT THIS ALSO

    [3] =>ANDTHIS

)
请注意,我需要删除所需值前后的所有内容


非常感谢您的帮助,谢谢

如果您只需要字符串,那么您的问题是针对每个字符串-检查针数组中的内容是否在该字符串中-如果是,则返回第一个找到的针元素。 可通过以下方式轻松实现:

$file = [
    'i dont need this NEED1 i dont need this',
    'crap crap crap',
    'i dont need this NEED2 i dont need this',
    'garbage garbage garbage',
    'i dont need this WANT THIS ALSO i dont need this',
    'unused unused unused',
    'i dont need this ANDTHIS i dont need this',
];

$needle = ['NEED1', 'NEED2', 'WANT THIS ALSO','ANDTHIS'];
$result = [];
array_map(function($item) use ($needle, &$result)
{
   //regex creation may be done before iterating array - that will save resources
   if(preg_match('/'.join('|', array_map('preg_quote', $needle)).'/i', $item, $matches))
   {
      $result[] = $matches[0];
   }
}, $file);
//var_dump($result);
$matches=[];
//真的不需要数组吗?
$getpositions=内爆(“”,$getpositions);
foreach($searchpoz作为$val){
$pos=strpos($getpositions,$val);
如果($pos!==false)$matches[$val]=$pos;
}
//保持发生顺序。
asort($matches);
打印(数组键($matches));

请发布原始阵列和所需输出。现在我把你的问题看作是-过滤针数组,不包括那些在原始数组中找不到的项原始数组是一个php文件,它使用file()将所有行放入和数组中,正如你在上面看到的,我只保留了包含特定字符串的行,但我不需要完整的行,我只需要stringSo-再次-如果您只需要string,那么您的问题是针对每个string-检查针数组中的内容是否在这个string中-如果是,返回第一个找到的针元素。我说得对吗?是的,在我取消设置不需要的行并用匹配的指针替换值后,我看到我需要一个else。说起来容易做起来难,但我坚持it@Benn它不需要这样做,除非你在用它做别的事情?(在这种情况下-只需重命名变量…)-这只是一个没有regex的替代方案。是的,只是注意到,好的一个和短的一个,似乎更快,但使者一个似乎更快,而不是100%在它上面