简体   繁体   English

使用foreach从数组中删除元素

[英]Removing element from an array using foreach

this is my array output 这是我的数组输出

Array ( [0] => Site=[121] [1] => #asasasas [2] => Devices=34343 [3] => DeviceID=[hjhghg])

Now I want to remove the value with key if it value contains # symbol... I have tried this, but does not seem to work.... 现在,我想用键删除该值,如果它的值包含#符号...我已经尝试过了,但是似乎不起作用....

foreach ($myarray as $key=>$value) {
    if (strpos($value,'#') !== false) {
        unset(($myarray[$key]);
    }
}

any ways to fix this?? 有什么办法解决这个问题?

Updated this is my actual array array_values($lines) i have already tried this... 更新这是我实际的数组array_values($lines)我已经尝试过了...

if(strpos($value,'#') !== false) {
                           unset(array_values($lines)[$key]);
                       }

unset后,您的括号中的括号过多

This works fine for me 这对我来说很好

$arr = array (  0 => 'Site=[121]',
        1 => '#asasasas',
        2 => 'Devices=34343', 
        3 => 'DeviceID=[hjhghg]'
      );
foreach($arr as $key => $val){
  if(strpos($val,'#') !== false) {
       unset($arr[$key]);
   }
}
print_r($arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM