dongxie3681
2013-05-14 14:59
浏览 602
已采纳

循环遍历数组中的字符串并将找到的字符串放入另一个数组[关闭]

Im trying to loop through the $files array and:

  1. Find occurrences of "some-string".
  2. For every "some-string" occurrence found, i need to add it to an array. (ie. $some_strings).
  3. Finally be able to call $some_string array outside of the for loop for manipulation(ie. count(), $some_string[1])

    foreach($files as $key=>$value)
    {
    if(strstr($value,'some-string')){
        $some_strings = array();
        $some_strings = $files[$key];
        unset($files[$key]);
    } elseif (strstr($value,'php')) {
        unset($files[$key]);
      }
    
    
    }
    

Every things seems to work fine until i try count($some_strings). This only returns 1 value when i know there are atleast 10 values. WHat am i doing wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulan1073 2013-05-14 15:04
    已采纳

    Try this

    $some_strings = array();
    foreach($files as $key=>$value)
    {
        if(strstr($value,'some-string')){
           $some_strings[] = $files[$key];
           unset($files[$key]);
        } elseif (strstr($value, 'php')) {
          unset($files[$key]);
        }
    }
    //Now you can use $some_strings here without a problem
    
    点赞 评论
  • dongxu1875 2013-05-14 15:04

    Try this

     foreach($files as $key=>$value)
     {
       if(strstr($value,'some-string'))
       {
         $some_strings[$key] = $value;
          unset($files[$key]);
       } elseif (strstr($value,'php')) 
       {
         $another_strings[$key] = $value;
          unset($files[$key]);
       }
     }
    
     echo count( $some_strings);
     echo count( $another_strings);
    
    点赞 评论

相关推荐 更多相似问题