關於PHP數組的幾個小技巧

咱們知道在PHP7.1中多了幾個對數組使用的新特性。php

這裏咱們舉兩個實際的例子吧:數組

例子一:post

// PHP 7.1+
$options = ['enabled' => true, 'compression' => 'gzip'];
['enabled' => $enabled, 'compression' => $compression] = $options;

var_dump($enabled);  // true
var_dump($compression);  // gzip

仔細體會。code

例子二:ip

$members = [
    [1, 'Seb'],
    [2, 'Alex'],
    [3, 'Brent'],
];

foreach ($members as [$id, $name]) {
    // do something with $id and $name
}

看到了吧,能夠直接在循環中指定變量,而後在循環體中來使用,是否是很簡單?get

好了本文就這些內容了,歡迎繼續關注。it

更多PHP知識,請前往 PHPCastsio

相關文章
相關標籤/搜索