ksort

ksort — 对数组按照键名排序

$fruits = array(“d”=>”lemon”, “a”=>”orange”, “b”=>”banana”, “c”=>”apple”);
ksort($fruits);
foreach ($fruits as $key => $val) {
echo “$key = $val\n”;
}

a = orange
b = banana
c = apple
d = lemon

array_slice

array_slice — 从数组中取出一段

$input = array(“a”, “b”, “c”, “d”, “e”);

$output = array_slice($input, 2); // returns “c”, “d”, and “e”
$output = array_slice($input, -2, 1); // returns “d”
$output = array_slice($input, 0, 3); // returns “a”, “b”, and “c”

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));

is_callable

is_callable — 检测参数是否为合法的可调用结构

function someFunction()
{
}

$functionVariable = ‘someFunction’;

var_dump(is_callable($functionVariable, false, $callable_name)); // bool(true)

echo $callable_name, “\n”; // someFunction

//
// Array containing a method
//

class someClass {

function someMethod()
{
}

}

$anObject = new someClass();

$methodVariable = array($anObject, ‘someMethod’);

var_dump(is_callable($methodVariable, true, $callable_name)); // bool(true)

echo $callable_name, “\n”; // someClass::someMethod

http_build_query

http_build_query — 生成 URL-encode 之后的请求字符串

$data = array(‘foo’=>’bar’,’baz’=>’boom’,’cow’=>’milk’,’php’=>’hypertext processor’);

echo http_build_query($data) . “\n”;
echo http_build_query($data, ”, ‘&’);

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor

stripos

stripos — 查找字符串首次出现的位置(不区分大小写)

$findme = ‘a’;
$mystring1 = ‘xyz’;
$mystring2 = ‘ABC’;

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// ‘a’ 当然不在 ‘xyz’ 中
if ($pos1 === false) {
echo “The string ‘$findme’ was not found in the string ‘$mystring1′”;
}

// 注意这里使用的是 ===。简单的 == 不能像我们期望的那样工作,
// 因为 ‘a’ 的位置是 0(第一个字符)。
if ($pos2 !== false) {
echo “We found ‘$findme’ in ‘$mystring2’ at position $pos2”;
}

copy

copy — 拷贝文件

$file = ‘example.txt’;
$newfile = ‘example.txt.bak’;

if (!copy($file, $newfile)) {
echo “failed to copy $file…\n”;
}

closedir

closedir — 关闭目录句柄

$dir = “/etc/php5/”;

// Open a known directory, read directory into variable and then close
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$directory = readdir($dh);
closedir($dh);
}
}