I have an array like this:
// Define pages
$pages = array(
"home" => array(
"title" => "Home page",
"icon" => "home"
),
"compositions" => array(
"title" => "Composition page",
"icon" => "music"
),
);
And what I am trying to accomplish is, having:
$navigation = Utils::makeNavigation($pages);
, create $navigation as an array of objects, so that I can parse it in my view
like this:
foreach($navigation as $nav_item){
echo $nav_item->page; // home(1st iter.), compositions(2nd iter.)
echo $nav_item->title;// Home page, Composition page
echo $nav_item->icon; // home, music
}
Is static Util-like-class approach good for this kind of problem?
EDIT
I came up with something like this, does this seem ok?
<?php
class Utils {
protected static $_navigation;
public static function makeNavigation($pages = array()){
if (!empty($pages)){
foreach ($pages as $page => $parts) {
$item = new stdClass;
$item->page = $page;
foreach ($parts as $key => $value) {
$item->$key = $value;
}
self::$_navigation[] = $item;
}
return self::$_navigation;
}
}
}
array_mapinstead. Does this count as an answer?array_map?