php常用函数
1、 in_array() ( 搜索数组中是否存在指定的值)
$sites = array("Google", "Runoob", "Taobao", "Facebook"); if (in_array("Runoob", $sites)) { echo "找到匹配项!"; } else { echo "没有找到匹配项!"; } 结果: 找到匹配项!
2、array_intersect() ( 比较两个数组的键值,并返回交集 )
$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 )
3、array_column() ( 返回输入数组中某个单一列的值 )
$a = array( array( 'id' => 5698, 'first_name' => 'Peter', '11' => 'Griffin', ), array( 'id' => 4767, 'first_name' => 'Ben', '22' => 'Smith', ), array( 'id' => 3809, 'first_name' => 'Joe', '33' => 'Doe', ) ); $last_names = array_column($a, 'last_name'); // 可能从数据库中返回数组 $last_names_id = array_column($a, 'last_name', 'id'); //从记录集中取出 last_name 列,用相应的 "id" 列作为键值: print_r($last_names); print_r($last_names_id); 结果: Array ( [0] => Griffin [1] => Smith [2] => Doe ) Array ( [11] => Griffin [22] => Smith [33] => Doe )
4、array_keys() ( 返回数组的键 )
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a,"Highlander")); 结果: Array ( [0] => Toyota )
5、array_values() ( 返回数组的值 )
$a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA"); print_r(array_values($a)); 结果: Array ( [0] => Peter [1] => 41 [2] => USA )
6、array_diff()
7、str_shuffle('1234567890') (随机打乱字符串,返回打乱后的新字符串)
8、shuffle(array) ( 随机打乱数组顺序,返回打乱后的新数组)
let the world have no hard-to-write code ^-^