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