Created
October 28, 2013 04:31
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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