-1

I'm trying to remove a key and value from an array, the thing is that i dont know where its located in the array (can be in random places - but only ONCE!) i found some posts here but seems like none of them could help my problem or i just didnt understood how it works:

PHP array delete by value (not key)

PHP using preg_match or regex as value for array_search or the key for array_keys_exist

the first link acctually worked for me, but the value cannot be regex and only static value.

anyone knows how i can remove from my array its key and it's value with regex? for example:

array1 = (dany,hello,tamtam,gogo,remove1)
expected array:
array1 = (dany,hello,tamtam,gogo)

after finishing with the first array its being unset

array1 = (lion,dear,damdam,lalala,remove2)
expected array:
array1 = (lion,dear,damdam,lalala)

Thanks in advance.

Current Redemption
  • 169
  • 1
  • 1
  • 11
  • It's unclear what you're trying to do here. You example shows two entirely different arrays with no intersecting values whatsoever. Is it really a simple, one-dimensional array you're working with? If so, `array_filter` will do the trick. – El_Vanja Mar 22 '20 at 13:31
  • could you please share expected input array and output array ? – Ronak Dhoot Mar 22 '20 at 13:32
  • @El_Vanja - seems like it didnt work. – Current Redemption Mar 22 '20 at 13:37
  • @RonakDhoot - sorry seems like i forgot to mention that, thanks! i've updated my question. – Current Redemption Mar 22 '20 at 13:38
  • @CurrentRedemption so all array with "remove" string needs to cleaned ? – Ronak Dhoot Mar 22 '20 at 13:39
  • @RonakDhoot - yes, but as you can see the "remove" can contains numbers thats why i want the regex, im checking Andre Oosthuizen solution meanwhile reading about array_key_exists – Current Redemption Mar 22 '20 at 13:41
  • No need for regex. [`strpos`](https://www.php.net/manual/en/function.strpos.php) is faster and can do the job for you. Check that within `array_filter` and you're done. – El_Vanja Mar 22 '20 at 13:43
  • @El_Vanja - can you give an example? seems like strpos is a search, but how can i delete what i wanted? since it can always be random number? – Current Redemption Mar 22 '20 at 13:48
  • What kind of syntax is this? Are you actually talking about text strings, or are these supposed to be PHP arrays (which do not exist in that syntax you wrote them in)? – ArSeN Mar 22 '20 at 13:50

3 Answers3

3

Gathering from the comments that you actually just want to remove all values that have the word remove in them, you can do this by using array_filter() with a custom callback:

$array1 = ['lion','dear','damdam','lalala','remove2'];

$output = array_filter($array1, function ($value) {
    return strpos($value, 'remove') === false;
});

var_dump($output);

output:

array(4) {
  [0]=>
  string(4) "lion"
  [1]=>
  string(4) "dear"
  [2]=>
  string(6) "damdam"
  [3]=>
  string(6) "lalala"
}
ArSeN
  • 5,133
  • 3
  • 19
  • 26
0

Without giving us an example of output required, it is difficult to try and give you a result, what you might be looking for is something like this -

function add_or_remove(&$array, $key, $value) {

    // remove key/value pairs if they're both identical
    if (array_key_exists($key, $array)
      && $array[$key] == $value) {
        unset($array[$key]);

    // add new key/value pair
    // OR modify the value for existing key
    } else {
        $array[$key] = $value;
    }
}
ArSeN
  • 5,133
  • 3
  • 19
  • 26
AlwaysConfused
  • 450
  • 4
  • 13
0

Although array_filter() is a good way of removing elements from an array, the one thing it can't deal with is removing 1 item from the array and then stopping (although you can use an exception to stop it). If there is a large array with one of the earlier elements being removed, array_filter() will always continue to scan the entire array.

Using a straight forward foreach() and a comparison allows you to unset() from the original array and stop as soon as this is done (using break)...

$array1 = ['lion','dear','damdam','lalala','remove2', 'fred'];

foreach ( $array1 as $key => $value )   {
    if ( strpos ( $value, 'remove' ) !== false )    {
        unset ( $array1 [ $key ] );
        break;
    }
}
print_r($array1);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55