-1

I have the example array :

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

By example, I want remove all element of this array beginning by "foo" and "faa". The expected result must be :

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

// some processes 

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

In my example, the $remove array contain 2 entries. But in my real case, this array may contains X entries.

How can I remove entries in main array ($a in the example) depending of the beginning of array's values (contained in another array, $removes in my example)

Thanks for help !

EDIT : I can do something like this :

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

But I think (maybe I'm wrong) there is a better solution (more proper, and more fast for php execution time). Maybe is there a way for avoid a double foreach

spacecodeur
  • 2,206
  • 7
  • 35
  • 71

4 Answers4

0

Here is something you can use. Edited for looping through array of 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;
    }

Frank
  • 243
  • 1
  • 2
  • 14
  • array_filter would probably be more efficient than manually creating a new array, `function removeFromArrayByPrefix($prefix, $haystack){return array_filter($haystack,function($v)use(&$prefix){return (false!==strpos($v,$prefix));});` – hanshenrik Jan 10 '19 at 10:18
  • I have modified my answer to accept array of prefixes to remove – Frank Jan 10 '19 at 10:23
0

Expanding on your current code you could use a basic for loop to go through both of the arrays and remove items as needed.

$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);
ypoulakas
  • 401
  • 4
  • 7
0

This may help you.

<?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);
?>

Thanks.

Ramya
  • 199
  • 10
0

You can use a simple foreach that filter this. The follow example use a custom function to allow a array as needle for the string search. The example filter all values with 'foo' and '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);

Result 1.

The test and result can you see in this online test: https://3v4l.org/cZQ84

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

2. Simple for search ONE string

If you will search only for one string is it much more easier, see below:

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

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

var_dump($r);

Result 2.

array(4) {
  [0]=>
  string(4) "faa2"
  [1]=>
  string(4) "fuu4"
  [2]=>
  string(4) "faa5"
  [3]=>
  string(4) "fuuX"
}
bueltge
  • 2,294
  • 1
  • 29
  • 40