Home  >  Article  >  Backend Development  >  Sharing the specific implementation method of deleting blank elements in an array in PHP_PHP Tutorial

Sharing the specific implementation method of deleting blank elements in an array in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:30:01942browse

The specific code for us to actually use PHP to delete empty elements in an array is as follows:

<ol class="dp-xml">
<li class="alt"><span><span>function array_remove_empty(& $arr, $</span><span class="attribute"><font color="#ff0000">trim</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">true</font></span><span>)   </span></span></li>
<li class=""><span>{   </span></li>
<li class="alt">
<span>foreach ($arr as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value) {   </span>
</li>
<li class=""><span>if (is_array($value)) {   </span></li>
<li class="alt"><span>array_remove_empty($arr[$key]);   </span></li>
<li class=""><span>} else {   </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">value</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">trim</font></span><span>($value);   </span>
</li>
<li class="">
<span>if ($</span><span class="attribute"><font color="#ff0000">value</font></span><span> == '') {   </span>
</li>
<li class="alt"><span>unset($arr[$key]);   </span></li>
<li class=""><span>} elseif ($trim) {   </span></li>
<li class="alt"><span>$arr[$key] = $value;   </span></li>
<li class=""><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>}   </span></li>
<li class="alt"><span>}  </span></li>
</ol>

The above code is the specific implementation method of deleting the blank elements of the array in PHP. I hope you can save it and collect it for future use. .


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446341.htmlTechArticleThe specific code we actually use PHP to delete empty elements in the array is as follows: functionarray_remove_empty($arr,$ trim = true ) { foreach($arras$ key = $value){ if(is_array($value)){ arr...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn