PHP Wants To Be Your Web Language Of The Future – Part 4 – Foreach Non-Scalar Keys and Using with List()

PHP foreach with non-scalar keys and listThis is part 4 of a 6 part series looking at the some of the wonderful features of PHP 5.5 and why it might remain a dominate web language for the future. This series was originally written for the Software Developer’s Journal.

In part 4 we are covering some of the new features related to foreach in the PHP 5.5 branch. While I myself have never really used these ideas much in my use of foreach, they are certainly welcomed additions to the language. The first idea is the use of non-scalar keys. Up until now the foreach loop dealt with keys that were pretty much scalar single values… like a string or an int. But now it can return keys which may be more comprehensive like an object. We also talk about the use of foreach when it comes to List(). Time to get started!

Foreach Can Now Accept Non-Scalar Keys

This change actually has a bit of design principles wrapped around it. When using the foreach construct you had to make sure that the values being returned as keys, from the iterable object, were of a scalar type. Native PHP arrays themselves can’t use a key that is non-scalar. Meaning that you had to use a string or an integer value as the key even if the value located at that key in the array was an object or some other type. Iterators on the other hand could define their own Key() method and essentially return any value as the key including an object. This proved difficult in using foreach which was looking for a scalar, not an object. This change now allows for the definition of different types to be returned from an iterable object to be used in a foreach loop.

This change means you could, in theory, make calls on returned keys in the foreach. Below is a piece of example code demonstrating this ability. While arbitrary, you can see how something like this might come into play for more advanced systems. In the code below, we set up an iterator class which we iterate through in a foreach. The iterator itself is returning, for its key, an object which we have stored as a value in an array. When the foreach requests the key, it will return the Shape object value as its key. The foreach can then call methods on this key showing you that it is indeed an object. We also ask the object (which is stored as the value) for its id proving that both $key and $value are objects which we can call methods on.


  // Custom shape class we will fetch from as a Key
  class Shape {
    private $id = 0;
    private $type = "";

    public function __construct($id, $shapeVal = "square") {
        $this->id = $id;
        $this->type = $shapeVal;

    public function getShapeType() {
        return $this->type;

    public function getId() {
        return $this->id;

 // Iterator which returns a key of type Shape.
 class myIterator implements Iterator {
    private $position = 0;
    private $array = array();  

    public function __construct() {
        $this->array = array(new Shape(1, "circle"), new Shape(2, "triangle"), new Shape(3));
        $this->position = 0;

    function rewind() {
        $this->position = 0;

    function current() {
         return $this->array[$this->position];

    function key() {
         return $this->array[$this->position];

    function next() {

    function valid() {
        return isset($this->array[$this->position]);

  // Notice that the key itself is an object.
 foreach (new myIterator() as $key => $value) {
    echo "Key is: " . $key->getShapeType() . " and value is: " . $value->getId() . "<br/>";


As you can see we are using the key as an object of type Shape and calling the getShapeType() method on it. This feature will allow developers to create object returning iterators that might be storing values using an object based key system. It is just another example of how the foreach construct has been beefed up to handle additional usage scenarios.

Foreach Can Now Use List()

By now you have seen a general trend with this PHP 5.5 release, the foreach and items that interact with the foreach have been given great focus. This feature, along with the other foreach statements all fit into the theme of cleaning up syntax. This addition is to use the list() construct inside a foreach to easily break data from an array into separate variables. This is an ideal situation for the database fetch() functions where you might be iterating over as a list of rows from a data table. Perhaps on each iteration you need to break the database row into several variables, one for each column. I find this addition to more syntax sugar than anything and not much more can be said about it.

In part 5 we are going to be talking about performance enhancement with Opacache and why this might be an inflection point in the language that will help speed up PHP created pages and make the over 70% of the web pages out there much faster. Visit back to check it out!

Thanks again for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.