|
導讀網頁的本質就是超級文本標記語言,通過結合使用其他的Web技術(如:腳本語言、公共網關接口、組件等),可以創造出功能強大的網頁。因而,超級文本標記語言是萬維網(Web)編程的基礎,也就是說萬維網是建立... 網頁的本質就是超級文本標記語言,通過結合使用其他的Web技術(如:腳本語言、公共網關接口、組件等),可以創造出功能強大的網頁。因而,超級文本標記語言是萬維網(Web)編程的基礎,也就是說萬維網是建立在超文本基礎之上的。超級文本標記語言之所以稱為超文本標記語言,是因為文本中包含了所謂“超級鏈接”點。 本篇文章主要給大家介紹如何用PHP給關聯數組進行排序。對于PHP學習者來說,數組是一個非常重要的知識點,所謂數組就是能夠在單獨的變量名中存儲一個或多個值。索引數組即帶有數字索引的數組,關聯數組即帶有指定鍵的數組,多維數組即包含一個或多個數組的數組。 下面我們就通過簡單的示例為大家介紹關聯數組進行各種排序的方法。 代碼示例如下: <?php
echo "Associative array : Ascending order sort by value";
$array2=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
asort($array2);
foreach($array2 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "Associative array : Ascending order sort by Key";
$array3=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
ksort($array3);
foreach($array3 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."";
}
echo "Associative array : Descending order sorting by Value";
$age=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
arsort($age);
foreach($age as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."";
}
echo "Associative array : Descending order sorting by Key";
$array4=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
krsort($array4);
foreach($array4 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
?>輸出結果如下: 1、按值升序 Associative array : Ascending order sort by value Age of Sophia is : 31 Age of William is : 39 Age of Ramesh is : 40 Age of Jacob is : 41 2、按照鍵名對關聯數組進行升序排序: Associative array : Ascending order sort by Key Age of Jacob is : 41 Age of Ramesh is : 40 Age of Sophia is : 31 Age of William is : 39 3、按值降序 Associative array : Descending order sorting by Value Age of Jacob is : 41 Age of Ramesh is : 40 Age of William is : 39 Age of Sophia is : 31 4、按照鍵名對關聯數組進行降序排序: Associative array : Descending order sorting by Key Age of William is : 39 Age of Sophia is : 31 Age of Ramesh is : 40 Age of Jacob is : 41 相關函數介紹: arsort() 函數對關聯數組按照鍵值進行降序排序。 asort() 函數對關聯數組按照鍵值進行升序排序。 krsort() 函數對關聯數組按照鍵名進行降序排序。 ksort() 函數對關聯數組按照鍵名進行升序排序。 本篇文章就是關于關聯數組的排序方法介紹,希望對需要的朋友有所幫助! 以上就是PHP怎么給關聯數組進行排序?(代碼示例)的詳細內容,更多請關注php中文網其它相關文章! 網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。 |
溫馨提示:喜歡本站的話,請收藏一下本站!