PHPで配列をソートするサンプル

phpコード:
方法1 配列をソート
function cmp($a, $b)
{
if($a==$b)
{
return 0;
}
return($a>$b)?-1:1;
}
$a=array(4,3,5,6,1);
usort ($a,"cmp");
foreach($a as $key=>$value)
{
echo “$key:$valuen";
}
方法2
配列をキーでソート
uksort(array,sorttype)
function cmp($a, $b)
{
if($a==$b)
{
return 0;
}
return($a>$b)?-1:1;
}
$a=array(4=>"four",3 =>"three",20 =>"twenty",10=>"ten");
uksort ($a,"cmp");
foreach($a as $key=>$value)
{
echo “$key:$valuen";
}
方法3 配列をsort()でソート
$fruits=array(“lemon","orange","banana","apple");
sort($fruits);
foreach($fruits as $key=>$val)
{
echo “$key=$valn";
}

PHP

Posted by arkgame