如何在循环中使用array_filter

时间:2016-12-01 11:32:31

标签: php arrays loops array-filter

我想在循环中过滤数组中的数据。我想知道的是我可以在内部循环中使用array_filter,因为我正在使用它并且它无法正常工作 这是我从DB获得的数组

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

我使用此代码

使数组唯一
$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,','),',');
}

运行此代码后,结果显示如下

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

但我想要的是从第一个数组中删除C我的意思是说不需要的字符串或字符串长度将小于2我想删除它。

预期结果:

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

我使用以下功能删除

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

但我认为因为它处于循环中而无法正常工作。我将此代码置于循环中的array_unique行之上。我想要的是删除长度小于2的值。

最后我已经达到了我想要的答案

    $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>';

它显示出我想要的完美结果

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

非常感谢大家的帮助

3 个答案:

答案 0 :(得分:1)

首先:我建议您不要将$listing$listings一起用作变量名,因为它很容易导致混淆,并且不是很好的可读性(特别是StackOverflow上的混淆)。 / p>

然后:您的代码中有错误。您没有检查长度(计数),但检查字符串本身是否为TRUE。

你有:

$NUM = 2;

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

你应该:

$NUM = 2;

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

答案 1 :(得分:0)

正如你的代码所说,$listings包含句子。如果我正确地解决了您的问题,那么您希望从$listings变量的每个句子中删除较小的句子。 您可以替换此代码:

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

使用以下代码块:

   $num = 2;
   $sentence = $listings[$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;
       }
   }
   $listings[$i] = implode(' ', $resSentence);

<强>更新 我已经看过下面的这个程序,是不是整个想你想要的?

<?php
$listing_cat = ['Chocolate Manufacturers,C', ',,,Chocolate Manufacturers,,Chocolate Manufacturers,,Chocolate Manufacturers', ''];
$num = 2;
for ($i = 0; $i < count($listing_cat); $i++) {

    $listing_cat[$i] = rtrim(ltrim($listing_cat[$i], ','), ',');
    $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] = implode($resSentence);
}
var_dump($listing_cat);

答案 2 :(得分:0)

您可以尝试以下代码:

function arr_filter($arr, $min_length) {
    define('STR_MIN_LEN', $min_length);
    $arr = array_filter($arr);
    $arr_explode = [];

    foreach($arr as $value) {
        $arr_explode = explode(',', $value);
        $arr_explode = array_unique(array_filter($arr_explode));
        $arr_explode = array_filter($arr_explode, function($element) {
            return strlen($element) >= STR_MIN_LEN;
        });
    }

    return array_values(array_unique($arr_explode));
}

var_dump(arr_filter($arr, 2));

以上代码是根据您的要求编写的。你可以尝试一下,看看它是否有效。此代码可能不灵活。您可以使用各种测试用例进行检查。希望它有效。

修改

我假设您有一个多维数组,如:

$arr = array(
    array(
        'Chocolate Manufacturers,C',
        ',,,Chocolate Manufacturers,,Chocolate Manufacturers,,Chocolate Manufacturers',
        ''
    ),
    array(
        'Ice Cream Manufacturers,C',
        ',,,Ice Cream Manufacturers,,Ice Cream Manufacturers,,Ice Cream Manufacturers',
        ''
    )
);

,代码是:

function arr_filter($arr, $min_length) {
    define('STR_MIN_LEN', $min_length);
    $final_arr = array();

    foreach($arr as $value) {
        $value = array_filter($value);
        $arr_explode = [];

        foreach($value as $another_value) {
            $arr_explode = explode(',', $another_value);
            $arr_explode = array_unique(array_filter($arr_explode));
            $arr_explode = array_filter($arr_explode, function($element) {
                return strlen($element) >= STR_MIN_LEN;
            });
        }

        $final_arr[] = array_values(array_unique($arr_explode));
    }

    return $final_arr;
}

var_dump(arr_filter($arr, 2));

我已添加此代码以使其适用于多维数组。希望它确实有效。

编辑2

function arr_filter($arr, $min_length) {
    define('STR_MIN_LEN', $min_length);
    $final_arr = array();

    foreach($arr as $value) {
        $value = array_filter($value);
        $arr_explode_final = [];

        foreach($value as $another_value) {
            $arr_explode = explode(',', $another_value);
            $arr_explode = array_filter($arr_explode, function($element) {
                return strlen($element) >= STR_MIN_LEN;
            });

            // the comma will be added if the string has two different words with comma-separated like `Chocolate Manufacturers, Ice Cream Manufacturers` else comma will be ommited
            $arr_explode_final[] = implode(',', array_unique($arr_explode)); 
        }

        $final_arr[] = $arr_explode_final;
    }
    return $final_arr;
}

var_dump(arr_filter($arr, 2));