您好,登錄后才能下訂單哦!
一、比較兩個數組之差:array_diff
例如:
$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 )
二、比較兩個數組的交集: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 )
三、取出數組中的那些數據:array_slice
array_slice默認是不保留鍵名的。
使用array_slice函數要保留鍵名值,最后一個參數$preserve_keys設置為true即可。
例如:
$levels = array('a' => 1, 'b' =>2, 'c' => 3, 'd' => 4, 'f' => 5, 'g' => 6);
$firstLevels = array_slice($levels, 0, 5, true);
結果為:
$firstLevels = array('a' => 1, 'b' =>2, 'c' => 3, 'd' => 4, 'f' => 5);
四、合并數組:array_merge()
例如:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
結果為:Array ( [0] => red [1] => green [2] => blue [3] => yellow )
五、取出二維數組中的某個字段:array_column()
例如:
$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_names = array_column($a, 'last_name');
print_r($last_names);
結果為:
Array
(
[0] => Griffin
[1] => Smith
[2] => Doe
)
六、將數組一維數組分成二維數組: array_chunk()
例如:
$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
)
)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。