【问题标题】:How do search array values and remove them based on a condition or string? [closed]如何根据条件或字符串搜索数组值并删除它们? [关闭]
【发布时间】:2013-04-25 19:25:09
【问题描述】:
  1. 从数组中读取文件名。
  2. 在数组值中搜索字符串“-dep”,如果任何字符串值包含“-dep”,则将其删除。
  3. 将数组作为值传递给另一个数组

.

for($d=0; $d < $files; ++$d)
{
    if(strpos($files[$d], "-dep") === true)
    {
      unset($files[$d]);
    }
}
return array($dnum, $fnum, $dirs, $files);

【问题讨论】:

  • 4.问题是什么?
  • $d &lt; $files 应该是$d &lt; count($files)
  • strpos 应该检查 &gt;= 0 因为它返回第一个匹配的索引。
  • 对不起,问题在标题中。我不知道这是不可接受的。
  • 想知道为什么这个问题被否决了吗?

标签: php arrays strpos


【解决方案1】:

您可以通过array_filter 做到这一点

【讨论】:

    【解决方案2】:

    我不知道这是否是最好的方法,但我会这样做

    foreach($array as $key=$value)
    {
        if(strstr($value, '-dep'))
            unset($array[$key]);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多