duanjue9296 2016-12-01 11:32
浏览 29

如何在循环中使用array_filter

I want to filter the data from the array in a loop. All i want to know is can I use the array_filter inside loop because i am using it and it is not working properly This is the array which i am getting from DB

   Array
    (
        [0] => Chocolate Manufacturers,C
        [1] => ,,,Chocolate Manufacturers,,Chocolate Manufacturers,,Chocolate Manufacturers
        [2] => 
    )

I am making the array unique by using this code

$num = 2;
for($i=0;$i<count($listing);$i++){

    //echo var_export($listing[$i]->cat_title).'<br>';
    $listing_cat[$i] = rtrim(ltrim($listing[$i]->cat_title,','),',');
    $listing_cat[$i] = explode(',', $listing_cat[$i]);
    //$listing_cat[$i] = array_filter(array_keys($listing_cat[$i]), function ($k){ return strlen($k)>=3; });
    $listing_cat[$i] = array_filter($listing_cat[$i], function ($sentence){
        //divide the each sentence in words
        $words = explode(',',$sentence);
        $resSentence = [];
        //check each words if their length is more then $num
        foreach($words as $word){
            if(strlen($word) > $num){
                $resSentence[] = $word;
            }
        }
        return implode(' ', $resSentence);
    });
    $listing_cat[$i] = array_unique($listing_cat[$i]);

    $listing_cat[$i] = implode(',', $listing_cat[$i]);
    $listing_cat[$i] = rtrim(ltrim($listing_cat[$i],','),',');
    //$tags['title'][$i] = rtrim(ltrim($listing[$i]->cat_title,','),',');
}

After running this code result is showing like this

   Array (
    [0] => Chocolate Manufacturers,C
    [1] => Chocolate Manufacturers
    [2] => 
   )

But what i want is to remove the C from the first array i mean to say the unwanted string or string which length will be less than 2 i want to remove that.

Expected Result:

Array (
        [0] => Chocolate Manufacturers
        [1] => Chocolate Manufacturers
        [2] => 
       )

i used the below function to remove

$NUM = 2;
$listings[$i] = array_filter($listings[$i], function ($element){ 
        return ($element[$i] > $NUM);
        }); 

But i think because it is in loop it is not working properly. I am placing this code above the array_unique line in loop. All i want is to remove the value which length will be less than 2.

Finally i have achieved my desired answer

    $num = 2;

for ($i = 0; $i < count($listing); $i++) {
    //$listing_cat[$i] = array_unique($listing[$i]->cat_title);
    $listing_cat[$i] = rtrim(ltrim($listing[$i]->cat_title, ','), ',');

    $sentence = $listing_cat[$i];

    //divide the each sentence in words
    $words = explode(',', $sentence);
    $resSentence = [];

    //check each words if their length is more then $num
    foreach ($words as $word) {
        if (strlen($word) > $num) {
            $resSentence[] = $word;
        }
    }
    $listing_cat[$i] = array_unique($resSentence);

    $listing_cat[$i] = implode(',',$listing_cat[$i]);

    $listing_cat[$i] = rtrim(ltrim($listing_cat[$i],','),',');
}

echo '<pre>';
print_r($listing_cat);
echo '</pre>';

it is showing perfect result what i want

Array (
        [0] => Chocolate Manufacturers
        [1] => Chocolate Manufacturers
        [2] => 
       )

Thanks all for help really appriciated

  • 写回答

3条回答 默认 最新

  • doujian3401 2016-12-01 11:36
    关注

    First of all: I recommend you not to use $listing along with $listings as variable names as it can easily lead to confusion and is not a good readability (especially confusing here on StackOverflow).

    Then: You have an error in your code. You are not checking for the length (count) but for the string itself which does resolve in TRUE.

    You have:

    $NUM = 2;
    
    $listings[$i] = array_filter($listings[$i], function ($element){ 
        return ($element[$i] > $NUM);
        }
    ); 
    

    You should have:

    $NUM = 2;
    
    $listings[$i] = array_filter($listings[$i], function ($element) use ($NUM) { 
        return (strlen($element[$i]) >= $NUM);
        }
    ); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目