从空值和仅包含一个单词的数组生成过滤器数组

I want to remove empty value in array and remove value that contain only one word.

This is my code:

$all = "smart lion, smart duck, small ant, , pig";
$allkey = explode (',',$all);
$allkey = array_unique($allkey);
for ($i = 0; $i < (count($allkey)); $i++)
{
    if (str_word_count($allkey[$i]) < 2)
    {
        unset($allkey[$i]);
    }
    else
    {
        echo $allkey[$i]."<br />";
    }
}

I want to remove empty value and word "pig", help me to make this code work. Thank you.

There is the code

$all = "smart lion, smart duck, small ant, , pig, pelican";
$allkey = explode (',',$all);
$allkey = array_unique($allkey);
for ($i = 0; $i < (count($allkey)); $i++)
{
    //we count the number of words
    $allwords = explode (' ',$allkey[$i]);
    //Remove empty words
    for ($j = 0; $j < (count($allwords)); $j++) {
        if ($allwords[$j]=="") unset($allwords[$j]);
    }
    if (count($allwords)<2)
        unset($allkey[$i]);
    else
        echo trim($allkey[$i])."
";
}

http://codepad.org/j6B2eEEX

<?php

$all = "smart lion, smart duck, small ant, , pig";
$allkey = explode (',',$all);
$allkey = array_unique($allkey);

$allkey = array_map( 'trim', $allkey );
$allkey = array_filter( $allkey );

var_dump( $allkey );

array_map walks through your array and executes the function trim which will remove beginning and trailing spaces. array_filter removes empty values as the evaluate to false.

Other way:

<?php   
$all = "smart lion, smart duck, small ant, , pig";
$allkey = explode (',',$all);
$allkey = array_unique($allkey);

foreach( $allkey as $key => $entry ) {
 $entry = trim( $entry );

 if( empty($entry) ) {
   unset( $allkey[$key] );
 }
}

var_dump( $allkey );

First of all, i would trim() inside the loop like

for ($i = 0; $i < (count($allkey)); $i++)
{
    $elem = trim($allkey[$i]);
}

Second, to skip values with less than two words, i'd write that into a separate array. So add before the for(...) loop:

$foundelems = array();

and then inside the for(...) loop, do that if:

if(str_word_count($elem) >= 2) {
    $foundelems[] = $elem;
}

and you have the right items in the $foundelems array. All together:

$all = "smart lion, smart duck, small ant, , pig";
$allkey = explode (',',$all);
$allkey = array_unique($allkey);
$foundelems = array();
for ($i = 0; $i < (count($allkey)); $i++)
{
    $elem = trim($allkey[$i]);
    if(str_word_count($elem) >= 2) {
        $foundelems[] = $elem;
    }
}

You can achieve exactly what you want with a single call to array_filter with a custom callback function:

$all = "smart lion, smart duck, small ant, , pig";
// get array of unique, trimmed elements
$exploded = array_unique(array_map('trim', explode(',', $all)));
$allkey = array_filter($exploded, function($el) {
  $val = trim($el);
  // return false if empty or only one word, which will remove the element from the array
  return (!empty($val) && str_word_count($val) >= 2);
});

You can do as follows:

$arr = explode(',',"smart lion, smart duck, small ant, , pig");
foreach($arr as $ky => $val) {
    if (strpos(trim($val),' ') === false) {
            unset($arr[$ky]);
        }
}
var_dump($arr);

Above echoes:

array(3) { [0]=> string(10) "smart lion" [1]=> string(11) " smart duck" [2]=> string(10) " small ant" }