I’m trying to nicely output a data array (with Kohana v2.3.4), and am thinking there has to be a more efficient and elegant way to do this. My array looks like this:
array('category_id' => value, 'category_title' => value, 'posts' => array( 'id' => value, 'title' => value, ... ))
And here’s how I’m outputting it in my view (some array values are omitted from this example for the sake of simplicity):
foreach($data as $d) {
echo '<h3>'.$d['category_title'].'</h3>';
foreach($d['posts'][0] as $p) {
echo '<p>'.$p['title'].$p['id'].'</p>';
}
}
Is there a better way to go about this with the array I have?
You can’t escape from using nested loop (unless if you use array_walk etc) but you can make do without using lots of string concatenation by taking advantage of variable substitution:
You can also combine it with extract() for cleaner strings: