3 Cool Things You Can Do With Arrays In PHP

PHP has a wide variety of functions for interacting with arrays. Some of them are more known than others. In this short blog, I will demonstrate 3 cool things you can do with arrays in PHP that you may or may not know about.

Turn variables into arrays

The first thing I want to show is how to use compact() to turn variables into an array. I never heard of it before using Laravel, but now I use it all the time – especially in my controllers when I need to pass variables to a view.

php > $name = 'Peter';
php > $email = 'peter@suhm.dk';
php > $array = compact('name', 'email');
php > var_dump($array);
array(2) {
  ["name"]=>
  string(5) "Peter"
  ["email"]=>
  string(13) "peter@suhm.dk"
}

Turn arrays into variables

The opposite of compact() is extract(), which lets you turn an array into a bunch of variables.

php > $array = array('name' => 'Peter', 'email' => 'peter@suhm.dk');
php > extract($array);
php > var_dump($name);
string(5) "Peter"
php > var_dump($email);
string(13) "peter@suhm.dk"

Implement ArrayAccess

Finally, I want to demonstrate the ArrayAccess interface that comes with PHP. It is really awesome and lets you take arrays to the next level. If you need inspiration, check out the Collection class from Laravel. By implementing ArrayAccess, your objects can be accessed like they were an array, which is pretty awesome.

class AwesomeClass implements ArrayAccess
{
    // Example from php.net
    // http://php.net/manual/en/class.arrayaccess.php
    private $container = [];

    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }

    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
}

// Usage
$awesome = new AwesomeClass;

$awesome['name'] = 'Peter';

2 thoughts on “3 Cool Things You Can Do With Arrays In PHP”

  1. Hey Peter,

    extract() is actually used a lot in the WP core. Though I never heard about the compact() functions, thanks for sharing that! Having said that, using both of this functions is a very bad practice . When using extract(), IDEs will alert that the variables not exist since there’s no explicit declaration, plus the refactoring of the variable names wouldn’t work. Same thing regarding compact, if you’ll want to refactor the variable name, the IDE won’t know to modify the strings in the array representing the variable names.

    PHP provide a lot of “magic” which is really cool. On the other hand, it’s not a good practice to use magic in coding 🙂

    1. I don’t agree that something is bad practice, just because it’s not supported by an IDE or because it’s “magic”. I believe that some of the magic in PHP is what makes it really awesome. It’s up to the developer to be responsible and educated IMO.

      Btw. I’m pretty sure PhpStorm can figure out `extract()`. At least it can with `compact()`! It’s a really awesome IDE.

Leave a Reply

Your email address will not be published. Required fields are marked *