1. 程式人生 > 實用技巧 >PHP學習之五:陣列(二)定位陣列元素

PHP學習之五:陣列(二)定位陣列元素

定位陣列元素

  1、搜尋陣列

  in_array() 函式在陣列中搜索一個特定值,如果找到這個值則返回true,否則返回false

$states = array("Ohio","Florida","Texas");
$state = "Florida";
if(in_array($state,$states)){
    echo "Found:",$state;
}
else {
    echo "Not Found.";
}

  2、搜尋關聯陣列鍵

  array_key_exists() 搜尋一個關聯陣列中的鍵值,如果搜到到則返回true,否則返回false

$states
["oh"] = "Ohio"; $states["fl"] = "Florida"; $states["te"] = "Texas"; $state = "oh"; if(array_key_exists($state,$states)){ echo "Found:",$state; } else { echo "Not Found."; }

  3、搜尋關聯陣列值

  array_search() 搜尋陣列中一個指定的值,找到則返回true,否則返回false

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states
["te"] = "Texas"; $state = "Florida"; if(array_search($state,$states)){ echo "Found:",$state; } else { echo "Not Found."; }

  4、獲取陣列鍵

  array_keys() 函式返回一個數組,其中包含陣列中所有的鍵值。

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states["te"] = "Texas";
$keys = array_keys($states);
print_r($keys
); //Array ( [0] => oh [1] => fl [2] => te )

  5、獲取陣列值

  array_values() 函式返回一個數組中所有的值,並自動新增數值索引。

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states["te"] = "Texas";
$keys = array_values($states);
print_r($keys); //Array ( [0] => Ohio [1] => Florida [2] => Texas )

  6、遍歷陣列

    1)、獲取當前陣列鍵

    key() 函式返回陣列中當前指標所在的位置的陣列的鍵

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states["te"] = "Texas";
while($key = key($states))
{
    echo $key,"<br/>";
    next($states);
}
//輸出
//oh
//fl
//te

    2)、獲取當前陣列值

    current() 函式返回陣列中當前指標所在位置的陣列的值

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states["te"] = "Texas";
while($key = current($states))
{
    echo $key,"<br/>";
    next($states);
}
//輸出
//Ohio
//Florida
//Texas

    3)、移動陣列指標

    next() 函式將指標移動到下一個陣列的位置,並返回指標指向的值;

    prev() 函式將指標移動到上一個陣列的位置,並返回指標指向的值;

    reset() 函式將指標移動到陣列的第一個位置,並返回指標指向的值;

    end() 函式將指標移動到陣列的最後一個位置,並返回指標指向的值;

    4)、向函式傳遞陣列值

    array_walk() 函式將陣列中的各個元素迴圈逐個傳遞到使用者自定義函式(程式碼中是dosomething)中。使用者自定義函式必須接受兩個輸入引數,一個是表示陣列當前值$value,一個是表示當前鍵$key,還可以新增一個可選引數userdata。

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states["te"] = "Texas";

function dosomething($value,$key,$data)
{
    echo $key,"=>",$value,"=>",$data,"<br />";
}

array_walk($states,"dosomething","data1");

//輸出
//oh=>Ohio=>data1
//fl=>Florida=>data1
//te=>Texas=>data1

    array_walk_recursive() 函式可以將多維陣列傳遞到使用者自定義函式。

  7、確定陣列的大小和唯一性

    1)、確定陣列的大小

    count() 函式返回數字中值的總數。count() 有一個可選引數mode,設定為1時,將迭代統計陣列子陣列的數量,返回總數。

    sizeof() 函式是count() 函式的別名。

$states["oh"] = "Ohio";
$states["fl"] = "Florida";
$states["te"] = "Texas";
$states["ad"] = array("11","22");

echo count($states); //4
print "<br/>";
echo count($states,1); //6

    2)、統計函數出現的頻度

    array_count_values() 函式返回一個包含關聯鍵值對的陣列,統計陣列用值的出現次數。

$states["oh0"] = "Ohio";
$states["fl0"] = "Florida";
$states["te0"] = "Texas";
$states["oh1"] = "Ohio";
$states["fl1"] = "Florida";
$states["te1"] = "Texas";
$states["oh2"] = "Ohio";
print_r(array_count_values($states));

//輸出
//Array ( [Ohio] => 3 [Florida] => 2 [Texas] => 2 )

    3)、確定唯一陣列元素

    array_unique() 函式會刪除函式中所有的重複值,返回一個由唯一值組成的陣列。

$states["oh0"] = "Ohio";
$states["fl0"] = "Florida";
$states["te0"] = "Texas";
$states["oh1"] = "Ohio";
$states["fl1"] = "Florida";
$states["te1"] = "Texas";
$states["oh2"] = "Ohio";
print_r(array_unique($states));

//輸出
//Array ( [oh0] => Ohio [fl0] => Florida [te0] => Texas )

  8、陣列排序

  預設情況下,PHP的排序是按照英語指定的規則進行排序。如果需要按另外一種語言的約定進行排序,就要修改預設行為,可以用 setlocale() 函式設定本地化環境(locale)。

    1)、逆置陣列元素順序(就是倒轉陣列)

    array_reverse() 函式將陣列中的元素的順序逆置(就是倒轉陣列,從後往前重新排序)。函式有一個可選引數 preserve_keys,為true重新排列鍵值,否則鍵值保持不變,預設為true。

$states[] = "Ohio";
$states[] = "Florida";
$states[] = "Texas";

print_r(array_reverse($states)); //Array ( [0] => Texas [1] => Florida [2] => Ohio )
print "<br/>";
print_r(array_reverse($states,1)); //Array ( [2] => Texas [1] => Florida [0] => Ohio )

    2)、置換陣列的鍵和值

    array_flip() 函式置換陣列中的鍵和值。

    注意,如果值出現重複,則鍵值調換的時候,相同的鍵值會被覆蓋,保留一個唯一鍵,值(原來的鍵)保留最後一個。

$states[] = "Ohio";
$states[] = "Florida";
$states[] = "Texas";

print_r($states); //Array ( [0] => Ohio [1] => Florida [2] => Texas )
print "<br/>";
print_r(array_flip($states)); //Array ( [Ohio] => 0 [Florida] => 1 [Texas] => 2 )

    3)、陣列排序

    sort() 函式對陣列進行排序,有可選引數sort_flags,如下表。(sort_flags不是int型別嗎?)

sort_flags 取值說明
SORT_REGULAR 正常比較單元
SORT_NUMERIC 單元被作為數字來比較
SORT_STRING 單元被作為字串來比較
SORT_LOCALE_STRING 根據當前的區域(locale)設定來把單元當作字串比較

$states[] = 1;
$states[] = 2;
$states[] = 3;
$states[] = "A";
$states[] = "B";
$states[] = "a";
$states[] = "b";
sort($states);
print_r($states); //Array ( [0] => A [1] => B [2] => a [3] => b [4] => 1 [5] => 2 [6] => 3 )

    保持鍵值對條件下的排序,採用asort() 函式

$states[] = 1;
$states[] = 2;
$states[] = 3;
$states[] = "A";
$states[] = "B";
$states[] = "a";
$states[] = "b";
asort($states);
print_r($states); //Array ( [3] => A [4] => B [5] => a [6] => b [0] => 1 [1] => 2 [2] => 3 )

    以逆序進行排序,採用 rsort() 函式

$states[] = 1;
$states[] = 2;
$states[] = 3;
$states[] = "A";
$states[] = "B";
$states[] = "a";
$states[] = "b";
rsort($states);
print_r($states); //Array ( [0] => 3 [1] => 2 [2] => 1 [3] => b [4] => a [5] => B [6] => A )

    保持鍵值對關係的逆向排序,採用 arsort() 函式

$states[] = 1;
$states[] = 2;
$states[] = 3;
$states[] = "A";
$states[] = "B";
$states[] = "a";
$states[] = "b";
arsort($states);
print_r($states); //Array ( [2] => 3 [1] => 2 [0] => 1 [6] => b [5] => a [4] => B [3] => A )

    陣列自然排序,natsort() 提供了一種日常使用習慣的排序機制。

$states[] = "pic1.jpg";
$states[] = "pic2.jpg";
$states[] = "pic10.jpg";
$states[] = "pic20.jpg";
natsort($states);
print_r($states); //Array ( [0] => pic1.jpg [1] => pic2.jpg [2] => pic10.jpg [3] => pic20.jpg )

    不區分大小寫的自然排序,natcasesort() 函式提供了natsort() 函式的忽略大小寫版本

$states[] = "pic1.jpg";
$states[] = "Pic2.jpg";
$states[] = "pic10.jpg";
$states[] = "Pic20.jpg";
natcasesort($states);
print_r($states); //Array ( [0] => pic1.jpg [1] => Pic2.jpg [2] => pic10.jpg [3] => Pic20.jpg )

    按鍵,不是按值對陣列排序,ksort() 函式按照鍵進行排序,而非值

$states["A"] = "pic1.jpg";
$states["C"] = "Pic2.jpg";
$states["B"] = "pic10.jpg";
$states["D"] = "Pic20.jpg";
ksort($states);
print_r($states); //Array ( [A] => pic1.jpg [B] => pic10.jpg [C] => Pic2.jpg [D] => Pic20.jpg )

    按鍵的逆序進行排序,用krsort() 函式

$states["A"] = "pic1.jpg";
$states["C"] = "Pic2.jpg";
$states["B"] = "pic10.jpg";
$states["D"] = "Pic20.jpg";
krsort($states);
print_r($states); //Array ( [D] => Pic20.jpg [C] => Pic2.jpg [B] => pic10.jpg [A] => pic1.jpg )

    根據使用者自定義規則進行排序

    usort() 函式提供了一個種跟進使用者比較演算法進行排序的方法。

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1,2);

usort($a, "cmp");
foreach ($a as $key => $value) {
    echo "$key: $value\n";
}
print "<br/>";
print_r($a);