PHP,根据数组值的开头删除简单数组中的某些元素

时间:2019-01-10 10:01:44

标签: php

我有示例数组:

$a = [
    "foo1",
    "faa2",
    "foo3",
    "fuu4",
    "faa5",
    "foo6",
    ...,
    "fuuX",
];

通过示例,我想删除此数组中以“ foo”和“ faa”开头的所有元素。预期结果必须是:

$removes = [
    "foo",
    "faa"
]

// some processes 

var_dump($a);
/* show  
$a = [
    "fuu4",
    ...,
    "fuuX",
];
*/

在我的示例中,$remove数组包含2个条目。但在我的实际情况下,此数组可能包含X个条目。

如何根据数组值的开头(在另一个数组中,在示例中为$a)来删除主数组(在示例中为$removes)中的条目

感谢帮助!

编辑: 我可以做这样的事情:

foreach($a as $key=>$entry){
    foreach($removes as $remove){
        if(strpos($entry, $remove) === 0){
            unset($a[$key]);
            break:
        }
    }
}

但是我认为(也许我错了)有一个更好的解决方案(更合适,并且对于PHP执行时间更快)。也许有一种方法可以避免两次foreach

4 个答案:

答案 0 :(得分:0)

这是您可以使用的东西。编辑用于循环遍历removes:


    public function doStuff()
    {
        $haystack = [
            "foo1",
            "faa2",
            "foo3",
            "fuu4",
            "faa5",
            "foo6",
            "fuuX",
        ];

        $removes = [
            "foo",
            "faa"
        ];

        $this->removeFromArrayByPrefixes($removes, $haystack);
    }

    protected function removeFromArrayByPrefixes($prefixes, $haystack)
    {
        $newData = [];
        foreach ($prefixes as $prefix) {
            foreach ($haystack as $key => $value) {
                // Check if that this iteration does not have the prefix
                if (strpos($value, $prefix) === false) {
                    // Prefix not found, add to return data
                    $newData[] = $value;
                }
            }
        }
        return $newData;
    }

答案 1 :(得分:0)

扩展当前代码,您可以使用基本的for循环遍历两个数组并根据需要删除项目。

$a = [
    "foo1",
    "faa2",
    "foo3",
    "fuu4",
    "faa5",
    "foo6",
    "fuuX",
];


$removes = [
    "foo",
    "faa"
];

// some processes 



foreach($a as $index => $item){

  foreach($removes as $removeItem){

//This is specifically looking for the $removes at the start of the string and not anywhere in the string like strpos would do.
if( mb_substr($item, 0, 3) == $removeItem){
            unset($a[$index]); 
            break;
      }

    }
}

var_dump($a);

答案 2 :(得分:0)

这可能会对您有所帮助。

<?php
$a = [
    "foo1",
    "faa2",
    "foo3",
    "fuu4",
    "faa5",
    "foo6",   
    "fuuX",
];
$removes = [
    "foo",
    "faa"
];
$collection = array();
foreach($a as $key => $value){
    foreach($removes as $val){
        if(substr($value, 0, strlen($val)) === $val){
            $collection[] = $value;
            continue 2;
        }
    }
}
print_r($collection);
?>

谢谢。

答案 3 :(得分:0)

您可以使用简单的foreach对此进行过滤。下面的示例使用自定义函数来允许数组作为字符串搜索的指针。该示例使用'foo'和'faa'过滤所有值。

$a = [
    "foo1",
    "faa2",
    "foo3",
    "fuu4",
    "faa5",
    "foo6",
    "fuuX",
];

function strposa($haystack, $needle, $offset=0) {
    if (!is_array($needle)) $needle = array($needle);
    foreach($needle as $query) {
        if (strpos($haystack, $query, $offset) !== false)
            return true; // stop on first true result
    }
    return false;
}

$r = [];
foreach($a as $key => $value){
    if(false === strposa($value, ['foo', 'faa'])){
         $r[] = $value;
    }
}

var_dump($r);

结果1。

您可以在此在线测试中看到测试和结果:https://3v4l.org/cZQ84

array(2) {
  [0]=>
  string(4) "fuu4"
  [1]=>
  string(4) "fuuX"
}

2。搜索一个字符串简单

如果您仅搜索一个字符串会更容易,请参见下文:

$a = [
    "foo1",
    "faa2",
    "foo3",
    "fuu4",
    "faa5",
    "foo6",
    "fuuX",
];

$r = [];
foreach($a as $key => $value){
    if(false === strpos($value, 'foo')){
         $r[] = $value;
    }
}

var_dump($r);

结果2。

array(4) {
  [0]=>
  string(4) "faa2"
  [1]=>
  string(4) "fuu4"
  [2]=>
  string(4) "faa5"
  [3]=>
  string(4) "fuuX"
}