PHP is one of the more commonly used programming technologies, so I studied PHP array deletion and shared it with you here. I hope it will be useful to everyone. PHP is a server-side scripting language that is easy to learn and use. With very little programming knowledge you can use PHP to build a truly interactive WEB site.
PHP array deletion code:
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li class=""><span>* 从数组中删除空白的元素(包括只有空白字符的元素) </span></li> <li class="alt"><span>* </span></li> <li class=""><span>* @param array $arr </span></li> <li class="alt"><span>* @param boolean $trim </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>复制代码 代码如下: </span></li> <li class=""> <span>function array_remove_empty(& $arr, $</span><span class="attribute">trim</span><span> = </span><span class="attribute-value">true</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>foreach ($arr as $</span><span class="attribute">key</span><span> =</span><span class="tag">></span><span> $value) { </span> </li> <li class="alt"><span>if (is_array($value)) { </span></li> <li class=""><span>array_remove_empty($arr[$key]); </span></li> <li class="alt"><span>} else { </span></li> <li class=""> <span>$</span><span class="attribute">value</span><span> = </span><span class="attribute-value">trim</span><span>($value); </span> </li> <li class="alt"> <span>if ($</span><span class="attribute">value</span><span> == '') { </span> </li> <li class=""><span>unset($arr[$key]); </span></li> <li class="alt"><span>} elseif ($trim) { </span></li> <li class=""><span>$arr[$key] = $value; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* 将一个二维数组转换为 hashmap </span></li> <li class=""><span>* </span></li> <li class="alt"><span>* 如果省略 $valueField 参数,则转换结果每一项为包含该项所有数据的数组。 </span></li> <li class=""><span>* </span></li> <li class="alt"><span>* @param array $arr </span></li> <li class=""><span>* @param string $keyField </span></li> <li class="alt"><span>* @param string $valueField </span></li> <li class=""><span>* </span></li> <li class="alt"><span>* @return array </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>复制代码 代码如下: </span></li> <li class=""> <span>function array_to_hashmap(& $arr, $keyField, $</span><span class="attribute">valueField</span><span> = </span><span class="attribute-value">null</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">ret</span><span> = </span><span class="attribute-value">array</span><span>(); </span> </li> <li class="alt"><span>if($valueField) { </span></li> <li class=""><span>foreach ($arr as $row) { </span></li> <li class="alt"><span>$ret[$row[$keyField]] = $row[$valueField]; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} else { </span></li> <li class=""><span>foreach($arr as $row) { </span></li> <li class="alt"><span>$ret[$row[$keyField]] = $row; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>return $ret; </span></li> <li class="alt"><span>} </span></li> </ol>