phpocean: Code is Clay
  • Tutorials
    • Front end
    • Back end
    • Computer skills
    • Design & illustration
    • See all »
  • Forum
  • Blog
  • Contact
  • Log In|Join free

Square bracket syntax for array destructuring assignment accepted

zooboole Published on May 9th, 2016 By zooboole

This is probably another great feature we're going to like in PHP 7.1. Since PHP 3 arrays have always been built with the array() syntax, in which we list its element by separating them with commas(;). And from PHP 5.4 it was possible to create an array with this $array = [] syntax.

As in PHP 3

<?php

// Creates an array containing elements with the values 1, 2 and 3, and keys numbered from zero
$array = array(1, 2, 3); 

// Creates an array containing elements with the values 1, 2 and 3, and the keys "a", "b", "c"
$array = array("a" => 1, "b" => 2, "c" => 3);

As in PHP 5.4

// Creates an array containing elements with the values 1, 2 and 3, and keys numbered from zero
$array = [1, 2, 3];

// Creates an array containing elements with the values 1, 2 and 3, and the keys "a", "b", "c"
$array = ["a" => 1, "b" => 2, "c" => 3];

This RFC mentioned one of the problems with the first syntax $array() is that it looks like a function call except the $ which makes the difference; Then the second option comes to eliminate that issue.

That is still a great way of constructing arrays. But what of destructing them, which is a way of assigning array elements to variables?

Since PHP 3 this was done by using the list() function in which we list the variables separated by commas like this:

<?php

// Assigns to $a, $b and $c the values of their respective array elements in $array with keys numbered from zero
list($a, $b, $c) = $array;

But in this case, the array must have numbered keys starting from zero, not for specific keys arrays, even though that has already been implemented in PHP 7.1. So this RFC suggested a cleaner way of destructuring arrays like following:

Assigns to $a, $b and $c the values of their respective array elements in $array with keys numbered from zero:

[$a, $b, $c] = $array;

Assigns to $a, $b and $c the values of the array elements in $array with the keys "a", "b" and "c", respectively:

["a" => $a, "b" => $b, "c" => $c] = $array;

This is very awesome because it establishes a symmetric relation between the array construction and the destructuring.

Let's wait for the 7.1 release and test this feature.


Twitter Facebook Google + Pinterest Linkedin

Advertisement

Don't miss any tutorial again

Receive fresh and new tutorials right at the moment they are published into your private email

Popular posts

8 cool features to come in PHP 7.1

8 cool features to come in PHP 7.1

Programming is fun and enjoyable and when a programming language makes development easy, coding becomes love.

about 1 year ago :: 26742 Read more
8 Tips to become a great PHP programmer in a week

8 Tips to become a great PHP programmer in a week

This tips can help you speed up your learning process in PHP and any other programming language.

over 2 years ago :: 11743 Read more
15 reasons why I love PHP

15 reasons why I love PHP

Far away from comparing PHP to any other languages which also have their respective qualities and fans, my approach is to show those reasons which make PHP a great choice for many people worldwide and also one of the most used in web development.

over 2 years ago :: 10055 Read more
Some 3 cool modern CMS for developers

Some 3 cool modern CMS for developers

Discover Modern Content Management Systems for Modern websites.

over 2 years ago :: 7726 Read more
6 Types of jobs you should never accept

6 Types of jobs you should never accept

As a web designer/developer, sometimes you have to handle things by yourself. You need to go out there and try to get jobs in order to make some income. That's normal, we all do it.

over 2 years ago :: 6838 Read more
Square bracket syntax for array destructuring assignment accepted

Square bracket syntax for array destructuring assignment accepted

PHP rfc: Square bracket syntax for array destructuring assignment accepted and will be possible from PHP 7.1

about 1 year ago :: 5583 Read more
Why Google's design material ?

Why Google's design material ?

Recently designers created a language that can be used to express and transmit feelings and thoughts. That is made of design elements and principles. They make up our painting, drawing, and express our mood.

over 3 years ago :: 5186 Read more

Recent posts

Clients think you are difficult

Clients think you are difficult

The difficulty he's talking about is not directed to us but to the tech field. It's challenging to set up and online business or to have a business which runs with technologies.

2 months ago Read more
What's the advantage of not using a CMS like WordPress?

What's the advantage of not using a CMS like WordPress?

Will it be adequate to build this project from scratch or I should use a CMS to make it faster? That's the question must of us ask all the time we start a new project.

2 months ago Read more
Why I(you) keep on growing smaller in programming?

Why I(you) keep on growing smaller in programming?

Let's admit it, most of us used to hate Maths back then in secondary school. Now that we are programmers we start to realize how limited we are without the Maths.

2 months ago Read more
Developer and project management: something you need to know

Developer and project management: something you need to know

When you are building a system which deals with money you learn the difference between serious projects and others. Check out my recent experience.

3 months ago Read more
Learning programming is different from learning a programming language

Learning programming is different from learning a programming language

You might be learning a programming language instead of programming itself. Read on to see why.

3 months ago Read more
Mirage of good code

Mirage of good code

When you write a code which gives you the impression of being correct, it's a mirage of code.

3 months ago Read more
Your client's dream is your nightmare

Your client's dream is your nightmare

Clients only have dreams. And, their dreams become your nightmares at the moment they finish dreaming and wake up.

3 months ago Read more
more »

Recent tutorials

  • How to disable the touchscreen drivers permanently on Ubuntu 17.10 March 11th, 2018
  • The science of creating fixed components on scroll with vanilla JavaScript February 13th, 2018
  • Easy guide on how set your workstation on Ubuntu 17.04 October 19th, 2017
more »

Advertisement
  • About us
  • Faq
  • Policy & Terms
  • Advertise with us
  • Contact
  • Discussions
  • Vialactea Technologies
Copyright © 2015 - 2018 phpocean.com. by Vialactea Technologies Ltd. Except where noted, all rights reserved - Proudly made in Ghana .:. Powered by zooboole.me.