I need to understand what is the best pattern for modifying data(be it collection of objects or array) which is passed from controller to view file.
Say I have 10 records of User entity. So array looks like this:
array(
0 => object User(),
1 => object User(),
2 => object User(),
3 => object User(),
etc...
);
And the view needs this data to look like this:
array(
0 => array(
0 => object User(),
1 => object User(),
),
1 => array(
0 => object User(),
1 => object User(),
),
etc...
)
So that users are grouped by two in sub-array.
Mind that this is just an example. I need to be able to modify data returned from query before i pass it to view. What is the best pattern for this?
UPDATE
May be i wasn't clear enough before. I'm asking about the PATTERN - in terms of OOP. What class pattern would contain this concrete function to format data for view.
Try this:
$a = array(0,1,2,3,4,5,6,7,8,9,10);
$b = array();
$c = count($a)/2;
for($i=0;$i<$c;$i++){
if($c%2 == 0) $b[$i] = array($a[$i*2], $a[$i*2+1]);
else if($c%2 != 0 && $i == floor($c)) $b[$i] = array($a[$i*2]);
else $b[$i] = array($a[$i*2], $a[$i*2+1]);
}
var_dump($b);