1.array_unshift 將資料推入陣列前面
$a=['1'];
array_unshift($a, 3);
print_r($a);
result: Array ( [0] => 3 [1] => 1 )
2.array_intersect 取陣列共同有的值,取交集
array_intersect($a1,$a2,$a3)
$a=['1',2];
$b=[2,1,'5'];
print_r(array_intersect($a,$b));
result: Array ( [0] => 1 [1] => 2 )
'1'和1也會交集到
3.call_user_func_array('method_name',[x,x,,x]) 手動call function
call_user_func_array('c', [1,2]);
function c($a,$b){
echo $a+$b;
}
// ['d','a'] d是類別a是類別的方法
call_user_func_array(['d','a'], [1,2]);
class d{
function a($a,$b){
echo $a-$b;
}
}
4.method_exists(物件,'方法名) 檢查物件裡是否有這個方法 return true or false
5.array_unique(array) 移除重覆值的鍵值對
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a)
);
6.php5.6 展開符 ...
<?php
$a=[1,2,3];
function a($a,$b,$c){
echo $a+$b+$c;
}
echo a(...$a); ///result=6
7. 5.6可以在常數用常數做相加等等動作或array
php
const ONE = 1;
const TWO = ONE * 2;
class C {
const THREE = TWO + 1;
const ONE_THIRD = ONE / self::THREE;
const SENTENCE = 'The value of THREE is '.self::THREE;
public function f($a = ONE + self::THREE) {
return $a;
}
}
echo (new C)->f()."\n";
echo C::SENTENCE;
?>
以上例程会输出:
4 The value of THREE is 3
现在可以通过 const 关键字来定义类型为 array 的常量。
<?php
const ARR = ['a', 'b'];
echo ARR[0];
?>
8.func_get_args 傳回方法裡的參數 array型式
function a($a){
print_r($a=func_get_args());
}
a(1,2,3,4); //
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )\n"; echo "Second: ". func_get_arg(1). "
\n"; echo "Third: ". func_get_arg(2). "
\n"; } return $numargs; }
$n = foo (10, 15, 20); //傳入 3 個參數 echo $n; ?>
First: 10 Second: 15 Third: 20 3
11. function(...) 展開運算符在方法裡 php5.6
<?php
function f($req, $opt = null, ...$params) {
// $params 是一个包含了剩余参数的数组
printf('$req: %d; $opt: %d; number of params: %d'."\n",
$req, $opt, count($params));
}
f(1);
f(1, 2);
f(1, 2, 3);
f(1, 2, 3, 4);
f(1, 2, 3, 4, 5);
?>
12.echo crypt('password','salt'); 基於des演算法的加密
PASSWORD_DEFAULT預設bcrypt算法
echo hash_equals($a,crypt('password','salt'));//1
__debugInfo() ¶
控制var_dump要輸出物件時的訊息
<?php
class a{
function __debugInfo(){
return ['shit'=>100];
}
}
$a=new a();
var_dump($a);
//object(a)#1 (1) { ["shit"]=> int(100) }