Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Easy Map

<?php
  
  class X {
     public function __construct(
	     public $id,
    	 public $name,
     ) {}
  }

  $list = [];
  foreach(range(0, 10) as $i) {
     $list[] = new X(rand(0, 1000_000), 'name '.$i);
  }
  
  $map = array_column($list, null, 'id');

  print_r($map)  ;
  
?>

array_column() extracts a property or an index in an array of objects or arrays. It also accepts null as second argument: this represents the original object or array, as a whole.

Combined with the third argument, it makes a convenient one-liner to build a map, based on a list of objects or arrays.

See Also

PHP Features

Last updated: 10 September 2026