PHP 常用函数 - 字符串函数
PHP 常用函数 - 数组函数
PHP 常用函数 - 数学函数
PHP 常用函数 - 目录、文件函数
PHP 常用函数 - 其他常用函数
提示:以下是本篇文章正文内容,下面案例可供参考
本篇文章参考菜鸟教程,仅供参考(常用函数就不写示例了,还有比较复杂的可参考菜鸟或官方手册)。
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c."; // I have several animals, a Dog, a Cat and a Horse.
$number = range(0,5);
print_r ($number); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
$people=array("Peter","Joe","Glenn","Cleveland");
echo key($people); // 0
$sites = array("Google", "Runoob", "Taobao", "Facebook");
var_dump(in_array("Runoob", $sites)); // bool(true)
$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people)); // Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$result = compact("firstname", "lastname", "age");
print_r($result); // Array ( [firstname] => Peter [lastname] => Griffin [age] => 41 )
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c"; // $a = Cat; $b = Dog; $c = Horse
$a=array("Volvo"=>"XC90","BMW"=>"X5");
var_dump(array_key_exists("Volvo", $a)); // bool(true)
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$firstKey = array_key_first($array);
echo $firstKey; // a
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$lastKey = array_key_last($array);
echo $lastKey; // c
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a)); // Array ( [0] => Volvo [1] => BMW [2] => Toyota )
$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]; // red
echo $a[$random_keys[1]]; // blue
echo $a[$random_keys[2]]; // brown
$a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
print_r(array_values($a)); // Array ( [0] => Peter [1] => 41 [2] => USA )
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a)); // Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
function myfunction($num) {
return($num*$num);
}
$a=array(1,2,3,4,5);
print_r(array_map("myfunction",$a)); // Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
$a=array("red","green");
print_r(array_pad($a,5,"blue")); // Array ( [0] => red [1] => green [2] => blue [3] => blue [4] => blue )
$a=array("red","green","blue");
array_pop($a);
print_r($a); // Array ( [0] => red [1] => green )
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a); // Array ( [0] => red [1] => green [2] => blue [3] => yellow )
function myfunction($v1,$v2) {
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction")); // -Dog-Cat-Horse
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_replace($a1,$a2)); // Array ( [0] => blue [1] => yellow )
$a1=array("a"=>array("red"),"b"=>array("green","blue"),);
$a2=array("a"=>array("yellow"),"b"=>array("black"));
print_r(array_replace_recursive($a1,$a2)); // Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black [1] => blue ) )
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a)); // Array ( [c] => Toyota [b] => BMW [a] => Volvo )
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a); // a
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a); // red
print_r ($a); // Array ( [b] => green [c] => blue )
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2)); // Array ( [0] => blue [1] => yellow [2] => brown )
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
array_splice($a1,0,2,$a2);
print_r($a1); // Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a)); // Array ( [a] => red [b] => green )
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a); // Array ( [0] => blue [a] => red [b] => green )
function myfunction($value,$key) {
echo "The key $key has the value $value
";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction");
// 输出:
The key a has the value red
The key b has the value green
The key c has the value blue
function myfunction($value,$key) {
echo "The key $key has the value $value
";
}
$a1=array("a"=>"red","b"=>"green");
$a2=array($a1,"1"=>"blue","2"=>"yellow");
array_walk_recursive($a2,"myfunction");
// 输出:
The key a has the value red
The key b has the value green
The key 1 has the value blue
The key 2 has the value yellow
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
print_r(array_change_key_case($age,CASE_UPPER)); // Array([PETER] => 35 [BEN] => 37 [JOE] => 43)
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
// 输出:
Array
(
[0] => Array
(
[0] => Volvo
[1] => BMW
)
[1] => Array
(
[0] => Toyota
[1] => Honda
)
[2] => Array
(
[0] => Mercedes
[1] => Opel
)
)
$a1=array_fill(3,4,"blue");
$b1=array_fill(0,1,"red");
print_r($a1);
print_r($b1);
// 输出:
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red )
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
print_r($a1); // Array ( [a] => blue [b] => blue [c] => blue [d] => blue )
function test_odd($var) {
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd")); // Array ( [3] => 3 )
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result); // Array ( [red] => a [green] => b [blue] => c [yellow] => d )
$a = array(
array(
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin',
),
array(
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith',
),
array(
'id' => 3809,
'first_name' => 'Joe',
'last_name' => 'Doe',
)
);
$last_names1 = array_column($a, 'last_name');
print_r($last_names1);
$last_names2 = array_column($a, 'last_name', 'id');
print_r($last_names2);
// 输出1:
Array
(
[0] => Griffin
[1] => Smith
[2] => Doe
)
// 输出2:
Array
(
[5698] => Griffin
[4767] => Smith
[3809] => Doe
)
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c); // Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2)); // Array([a] => red [b] => yellow [c] => blue)
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2)); // Array ( [a] => red [b] => Array ( [0] => green [1] => yellow ) [c] => blue )
$a=array(5,5);
echo(array_product($a)); // 25
$a=array(5,15,25);
echo array_sum($a); // 45
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result); // Array ( [d] => yellow )
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_diff_assoc($a1,$a2);
print_r($result); // Array ( [d] => yellow )
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_diff_key($a1,$a2);
print_r($result); // Array ( [b] => green )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
$result = array_diff_uassoc($a1,$a2,"myfunction");
print_r($result); // Array ( [a] => red [c] => blue )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_diff_ukey($a1,$a2,"myfunction");
print_r($result); // Array ( [c] => blue )
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2);
print_r($result); // Array ( [a] => red [b] => green [c] => blue )
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_intersect_assoc($a1,$a2);
print_r($result); // Array ( [a] => red [b] => green [c] => blue )
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_intersect_key($a1,$a2);
print_r($result); // Array ( [a] => red [c] => blue )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result); // Array ( [b] => green )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_intersect_ukey($a1,$a2,"myfunction");
print_r($result); // Array ( [a] => red [b] => green )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result); // Array ( [a] => red [b] => green )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"blue","c"=>"green");
$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result); // Array ( [b] => green [c] => blue )
function myfunction_key($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
function myfunction_value($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","c"=>"green");
$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result); // Array ( [c] => blue )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_uintersect($a1,$a2,"myfunction");
print_r($result); // Array ( [c] => blue )
function myfunction($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"blue","c"=>"green");
$result=array_uintersect_assoc($a1,$a2,"myfunction");
print_r($result); // Array ( [a] => red )
function myfunction_key($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
function myfunction_value($a,$b) {
if ($a===$b) {
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","c"=>"green");
$result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result); // Array ( [a] => red [b] => green )
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) ; // Peter
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people); // Peter
echo next($people); // Joe
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people); // Peter
echo next($people); // Joe
echo prev($people); // Peter
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people); // Peter
echo next($people) ;// Joe
echo reset($people); // Peter
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) ; // Peter
echo end($people); // Cleveland
$cars=array("Volvo","BMW","Toyota");
echo count($cars); // 3
$cars=array("Volvo","BMW","Toyota");
sort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "
";
}
// 输出:
BMW
Toyota
Volvo
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "
";
}
// 输出:
Volvo
Toyota
BMW
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
foreach($age as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
// 输出:
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
foreach($age as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
// 输出:
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
foreach($age as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
// 输出:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);
foreach($age as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
// 输出:
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35
function my_sort($a,$b) {
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
usort($a,"my_sort");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++) {
echo $a[$x];
echo "
";
}
// 输出:
2
4
6
8
function my_sort($a,$b) {
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
uksort($a,"my_sort");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
// 输出:
Key=a, Value=4
Key=b, Value=2
Key=c, Value=8
Key=d, Value=6
function my_sort($a,$b) {
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uasort($arr,"my_sort");
foreach($arr as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
// 输出:
Key=b, Value=2
Key=a, Value=4
Key=d, Value=6
Key=c, Value=8
$temp_files = array("temp15.txt","temp10.txt","temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "Standard sorting: ";
print_r($temp_files);
echo "
";
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
// 输出:
Standard sorting: Array ( [0] => temp1.txt [1] => temp10.txt [2] => temp15.txt [3] => temp2.txt [4] => temp22.txt )
Natural order: Array ( [0] => temp1.txt [3] => temp2.txt [1] => temp10.txt [2] => temp15.txt [4] => temp22.txt )
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "
";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
//上面代码将输出:
Natural order:
Array
(
[0] => Temp10.txt
[1] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[3] => temp15.txt
)
Natural order case insensitve:
Array
(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array); // Array ( [0] => blue [1] => green [2] => yellow [3] => red [4] => purple )
$a=array("Dog","Cat","Horse","Bear","Zebra");
array_multisort($a);
print_r($a); // Array ( [0] => Bear [1] => Cat [2] => Dog [3] => Horse [4] => Zebra )