0

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
  • I assume the final output you want is: Array ( [0] => Chocolate Manufacturers ) Right? Commented Dec 1, 2016 at 12:15
  • yes right this is what i want Commented Dec 1, 2016 at 12:16
  • I want to remove all the text which string length will be less than 2 Commented Dec 1, 2016 at 12:16

3 Answers 3

1

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);
    }
); 
Sign up to request clarification or add additional context in comments.

15 Comments

I'll provide a Fiddle in a moment.
@androidavid : " i mean to say the unwanted string or string which length will be less than 2 i want to remove that", this was needed so I think return (strlen($element[$i]) > $NUM); will be return (strlen($element[$i]) >= $NUM);
But it is not removing the C from the array
@NewbieJavaDeveloper You are right. But still the 'C' should be removed in my unedited post which the author says it does still appear so it does not solve the problem itself. But thanks :)
@jayantrawat Can you provide more information? I was trying to reproduce your code but there is a lot missing. I do not know where cat_title etc. comes from. Can you provide a working version of your code with a sample array so that I can provide you a solution?
|
0

You may try the following code:

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));

The above code is written as per your requirements. You could try it out and see if it works. This code may not be flexible. You can check it out with various test cases. Hope it works.

EDIT

I assume you have a multidimensional array like:

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

and the code is:

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));

I've added this code to make it work with multidimensional array. Hope it works for sure.

EDIT 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));

7 Comments

It works, But it remove the whole data from first array instead of removing the C only
Mention me what your final output with this code was and also the output you expect.
Here is the final output Array ( [0] => Array ( ) [1] => Array ( [0] => Chocolate Manufacturers ) [2] => Array ( ) )
The array you mentioned in the code was part of a multidimensional array?
I am sorry actually i have multiple index in one array
|
0

As you are code say, $listings is contains sentence. If I got your problem properly then, you want to remove smaller from each sentences of the $listings variable. You can replace your this code:

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

with the following codeblock:

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

Update I have check out this program below, is it the whole think what you want?

<?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);

13 Comments

Where i do place this code in loop or outside the loop and one more thing should i have to replace this code with my old code or have to add this with my code
1. you need to use use($num) to give the num into the functions and 2. You are just spitting 'Chocolate Manufacturers' into 'Chocolate' and 'Manufacturers' and check their length. How does this help? I am curious :)
Ops, there is no space in between , and C!! let me modify my code.
My problem is still the same it is not removing the C
This is the result of your code. It is also returning C Array ( [0] => Array ( [0] => Chocolate Manufacturers [1] => C ) [1] => Array ( [0] => Chocolate Manufacturers [2] => Chocolate Manufacturers [4] => Chocolate Manufacturers ) [2] => Array ( ) )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.