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 '
';
}
?>
相关文档:
<?php
define('SMARTY_TMP_DIR','C:/php5/Smarty-2.6.13/');
define('SMARTY_DIR','C:/php5/Smarty-2.6.13/libs/'); //SMARTY_DIR ->smarty keyword,must be defined as libs dectory
require_once(SMARTY_DIR.'Smarty.class.php');
//建立一个smarty对象
$smarty = new Smarty;
$smarty->template_dir = ......
Code:
<?php
function genRandomString($len)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", ......
<?php
require_once ('unit_tester.php');
require_once ('reporter.php');
require_once ('../config.php');
require_once ('../source\modules\user\user_api.func.php');
require_once ('../source/base_model.class.php');
Class registertest extends UnitTestCase{
public function testAssertEqual(){
$b ......
<?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 ......
在项目开发中发现对php的文档缺少管理,别人写了一个,功能不多
<?php
/**
* 类名: doc
*
描述: 文档生成类
* 其他: 可以对目录进行过滤,设置好源目录后,请用绝对路径指定生成目录,模式可调,模式
*
1为常规类型,即以 斜线**开头,以*斜线 结束
* 2为扩展类型,凡是 斜线*开头以*斜线 结束的部分都将成为文 ......