加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如果string包含任何字符,则从数组中删除元素

发布时间:2020-12-13 21:49:55 所属栏目:PHP教程 来源:网络整理
导读:如果字符串包含任何字符,则从数组中删除元素.例如,下面是实际的数组. array(1390) { [0]= string(9) "Rs.52.68"" [1]= string(20) ""php code generator"" [2]= string(9) ""Rs.1.29"" [3]= string(21) ""php codes for login"" [4]= string(10) ""Rs.70.23"
如果字符串包含任何字符,则从数组中删除元素.例如,下面是实际的数组.

array(1390) {
  [0]=>
  string(9) "Rs.52.68""
  [1]=>
  string(20) ""php code generator""
  [2]=>
  string(9) ""Rs.1.29""
  [3]=>
  string(21) ""php codes for login""
  [4]=>
  string(10) ""Rs.70.23""

 }

我需要数组来删除所有以RS开头的元素.

预期结果

array(1390) {
      [0]=>
      string(20) ""php code generator""
      [1]=>
      string(21) ""php codes for login""


     }

到目前为止我尝试了什么:

foreach($arr as $ll)
{

if (strpos($ll,'RS.') !== false) {
    echo 'unwanted element';
}

从上面的代码我如何从数组中删除不需要的元素.

解决方法

您可以在foreach循环中获取$key并在数组上使用unset():

foreach ($arr as $key => $ll) {
    if (strpos($ll,'RS.') !== false) {
        unset($arr[$key]);
    }
}

请注意,这将不会删除任何项目,因为“RS”永远不会出现.只有“Rs”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读