PHP_I love U之(1)php衣食父母:使用usort()函数为二维数组排序

WBOY
Release: 2016-06-23 13:44:43
Original
1616 people have browsed it

//PHP_I love U之(1)php衣食父母:使用usort()函数为二维数组排序
//PHP语言中使用usort()函数 为 二维数组排序:
// [][]==  a[][1]= 长度; a[][2] = views点击数

$AAlong=10; //变量$AAlong 定义数组长度
//for ($i=0;$i //{
for($j=0;$j {
$AA[$j][0]=$j;
$AA[$j][1]=rand(10,40);
$AA[$j][2]=rand(0,112);
}//for j22
//}//for11


//先显示一遍数组(原)值
for($j=0;$j {
echo "编号".$AA[$j][0].",长度".$AA[$j][1];
echo ",它的点击数".$AA[$j][2];
echo "\r\n";
}//for j22


//用usort( 数组变量, 排序函数() );   的方式排序!
usort($AA, function($a, $b) {
            $al = ($a[2]);  //不加括号当然可以, 加括号表示这里可以是表达式子,或函数等…
            $bl = $b[2];
            if ($al == $bl)
                return 0;
            return ($al > $bl) ? -1 : 1;  //这样就是正序(排序) 了 , 倒序呢?这样写: //return ($al         });


echo "\r\n再显示排序后的:\n";
for($j=0;$j {
echo "第".$j."个:编号".$AA[$j][0].",长度".$AA[$j][1];
echo ",它的点击数".$AA[$j][2];
echo "\r\n";
}//for j23


echo "ok success. I like php & usort()!";


// usort() 函数真是强大;  //也感谢 php 语言, 它保障了无数象我这样的码农... 让我们一家老小有饭吃!! :)

//有朋友说,你忘记 写  ?>” 来结束php封闭语言,俺是从Drupal养成的习惯 写 "?>" 会出错, 既然 不写 "?>"不会出错, 那我们就…必须写的时候再写“?>”吧!!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!