Tuesday, February 8, 2011

Sorting Array

Assalamu'alaikum and selamat sejahtera

This post will discuss on how to sort our array.So,before this we already learn the foreach loops,it will print all the key name and key value.So,sometimes,we may want to sort it.It very simple,we just use some special function that is:


sort($fname_of_your_array);
  
another sorting function:

rsort( ) – Sorts a array in reverse order
arsort( ) - Sorts the alphabet array in reverse order
krsort( ) - Sorts the Keys in an array in reverse order

here some proof for asort():
figure 1: Before using asort()
figure 1.1: After using asort()


The code:

<html>

<head>

<title>Foreach loops Array</title>

<?PHP

$squad_name = array();

$squad_name["alpha"] = "wing";
$squad_name["beta"] = "tail";
$squad_name["charlie"] = "head";
$squad_name["delta"] = "backup";

asort($squad_name);

foreach ($squad_name as $squad_name => $squad_code) {
print "Squad Name = " . $squad_name . " Squad Code = " . $squad_code . "<BR>";
}

?>

</head>

<body>

</body>

</html>

N/NOTE:LOL


No comments:

Post a Comment