A few tips about PHP arrays

We know that in PHP 7.1 there are several new features for using arrays.

Here are two practical examples:

Example one:

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

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

Experience it carefully.

Example two:

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

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

See, you can specify variables directly in the loop, and then use them in the loop body, isn't it very simple?

Well, that's all for this article, welcome to continue to pay attention.

For more PHP knowledge, go to PHPCasts

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325254822&siteId=291194637