Skip to content

Instantly share code, notes, and snippets.

@samjarrett
Created October 28, 2013 04:31
Show Gist options
  • Save samjarrett/012f8bc0cef374c2ac35 to your computer and use it in GitHub Desktop.
Save samjarrett/012f8bc0cef374c2ac35 to your computer and use it in GitHub Desktop.
#melbsf2 Symfony Components: Using the PropertyAccess component (see http://samjarrett.github.io/Symfony2-Components-Presentation/pres.html)
<?php
use Symfony\Component\PropertyAccess\PropertyAccess;
function getData($items, $columns) {
$accessor = PropertyAccess::createPropertyAccessor();
$rows = array();
foreach ($items as $item) {
$rows[] = array_map(function ($path) use ($item, $accessor) {
return $accessor->getValue($item, $path);
}, $columns);
}
return $rows;
}
$allowedColumns = array('[firstName]', '[lastName]');
$data = array(
array('id' => 1, 'firstName' => 'Paul', 'lastName' => 'Stanley'),
array('id' => 2, 'firstName' => 'Gene', 'lastName' => 'Simmons'),
array('id' => 3, 'firstName' => 'Ace', 'lastName' => 'Frehley'),
array('id' => 4, 'firstName' => 'Peter', 'lastName' => 'Criss'),
);
$viewData = getData($data, $allowedColumns);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment