Site icon 腳本酒

php array(1)

php排除陣列中重複的值

$result=[“s”,”s”,”23″];
array_unique($result)
資源網址
 

php陣列新增

array_push($result,”sss”,”sss”)
在第二個開始打要新增的變數或字串
資源網址

php交集和差集

交集array_intersect()  :
<?php    
$array1 = array ("a" => "green", "red", "blue");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_intersect ($array1, $array2);
?>

最後$result 變成:

Array
(
   [a] => green
   [0] => red
)
找兩個陣列中共通元素

差集 array_diff():

<?php
$array1 = array ("a" => "green", "red", "blue", "red");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_diff ($array1, $array2);
?>
結果是blue
找跟別的陣列中沒有的元素
資源網址
 
php array(2) php array(3)

Exit mobile version