Rumah >pembangunan bahagian belakang >tutorial php >利用函数解决PHP数组删除方案分析_PHP教程
PHP是比较常用的编程技术之一,于是我研究了一下PHP数组删除,在这里拿出来和大家分享一下,希望对大家有用。PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的WEB站点。
PHP数组删除代码:
<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>