PHP經常使用函數(三)

array_unique() 函數:

array_unique() 函數移除數組中的重複的值,並返回結果數組。當幾個數組元素的值相等時,只保留第一個元素,其餘的元素被刪除。返回的數組中鍵名不變。php

<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>

結果:
Array ( [a] => red [b] => green )

preg_ split() 函數用於正則表達式分割字符串

$split = preg_split('/\s+/', $post['codes']);
foreach ($split as $k => $v) {
    $v = trim($v);
    if(empty($v)){
        unset($split[$k]);
    }
}


實例:
<?php
$str = "php mysql,apache ajax";
$keywords = preg_split("/[\s,]+/", $str);
print_r($keywords);
?>

結果以下:
Array
(
    [0] => php
    [1] => mysql
    [2] => apache
    [3] => ajax
)
相關文章
相關標籤/搜索