Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://www.php.net/array_filter" rel="nofollow"><code>array_filter</code></a>, when passed no second parameter, will remove any empty entries. From there you can proceed as normal:</p> <pre><code>foreach (array_filter($list) as $each){ $places .= $each . ','; } </code></pre> <p>Though you can also use <a href="http://php.net/manual/en/function.implode.php" rel="nofollow"><code>implode</code></a> if you're just turning it in to a CSV:</p> <pre><code>$places .= implode(',', array_filter($list)); </code></pre> <p><strong>Side Note</strong> Though in this case <code>array_filter</code> may work, it is worth noting that this removes entries that result in a <a href="http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting" rel="nofollow">"falsy" result</a>. That is to say:</p> <pre><code>$list = array_filter(array('foo','0','false','')); // Result: // array(2) { // [0]=&gt; // string(3) "foo" // [2]=&gt; // string(5) "false" // } </code></pre> <p>So be careful. If the user could potentially be entering in numbers, I would stick with comparing <code>empty</code>. Alternatively you can use the second parameter of <code>array_filter</code> to make it more explicit:</p> <pre><code>function nonEmptyEntries($e) { return ((string)$e) !== ''; } $list = array_filter($list, 'nonEmptyEntries'); // result: //array(3) { // [0]=&gt; // string(3) "foo" // [1]=&gt; // string(1) "0" // [2]=&gt; // string(5) "false" //} </code></pre> <p>(Note that the <code>0</code> entry is kept, which differs from a blanket <code>array_filter</code>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload