PHP中多维数组的排序
1.用户定义排序:usort($array, functionName);其中functionName为用户定义的函数名,用户定义的函数指定排序规则,比较数组中两个元素的大小,大于返回正数,小于返回负数,等于返回0。2.反向用户排序:用户定义函数时,比较数组中两个元素的大小,大于返回负数,小于返回正数,等于返回0。$fruits = array(array('APP', 'Apple', 3.2), array('ORG', 'Orange', 2.6),
array('JIM','linina','3.4'));
echo 'Init:
';
foreach($fruits as $key)
{
echo $key['2'];
}
//按名称排序
function compareByName($x, $y) {
if ($x[1] == $y[1]) {
return 0;
}
else if ($x[1] > $y[1]) {
return 1;
} else {
return -1;
}
}
usort($fruits, 'compareBYName');
echo '
Compare By Name:
';
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
echo '|'.$fruits[$i][$j];
}
echo '
';
}
//按价格排序
function compareByPrice($x, $y) {
if ($x[2] == $y[2]) {
return 0;
}
else if ($x[2] > $y[2]) {
return 1;
}
else {
return -1;
}
}
usort($fruits, 'compareBYPrice');
echo '
Compare By Price:
';
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
echo '|'.$fruits[$i][$j];
}
echo '
';
}
//按价格反向排序
function reverse_compareByPrice($x, $y) {
if ($x[2] == $y[2]) {
return 0;
}
else if ($x[2] > $y[2]) {
return -1;
}
else {
return 1;
}
}
usort($fruits, 'reverse_compareByPrice');
echo '
Reverse Compare By Price:
';
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
echo '|'.$fruits[$i][$j];
}
echo '
';
}
?>
相关文档:
run_time.php Code:
<?php
class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
function start()
{
$this->StartTime = $this->get_microtime();
}
f ......
<?php
if($_POST['str'])
{
header('Content-type: application/doc');
header('Content-Disposition: attachment; filename="downloaded.doc"');
echo iconv("UTF-8","GB2312",$_POST['str']);
}
?>
<a href="javascript:void(0)" onclick="downword()">下载</a>
<div id="word" style="dis ......
最近想安装PEAR(PHP Extension and Application Repository),但是在执行批处理文件go-pear.bat的时候出现了错误:
phar "C:\php\PEAR\go-pear.phar" does not have a signature
PHP Warning: require_once(phar://go-pear.phar/index.php): failed to open stream: phar error: ......
在项目开发中发现对php的文档缺少管理,别人写了一个,功能不多
<?php
/**
* 类名: doc
*
描述: 文档生成类
* 其他: 可以对目录进行过滤,设置好源目录后,请用绝对路径指定生成目录,模式可调,模式
*
1为常规类型,即以 斜线**开头,以*斜线 结束
* 2为扩展类型,凡是 斜线*开头以*斜线 结束的部分都将成为文 ......