0

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;
        }
    }
}
4
  • What are your criterias to separate "good" from "bad"? Commented Oct 8, 2014 at 23:51
  • What is "ok"? Can it be measured objectively? What is "ok" for one, is "not ok" for another. It's "not ok" for me - I'd use array_map instead. Does this count as an answer? Commented Oct 9, 2014 at 0:14
  • Could you give small example regarding my situation, on how would you do it using array_map? Commented Oct 9, 2014 at 11:39
  • I would use it instead of a loop Commented Oct 9, 2014 at 11:43

2 Answers 2

1

Assuming you are creating the array manually in your code, just cast to objects:

$pages = array(
    "home" => ( object ) array(
        "title" => "Home page",
        "icon" => "home"
    ),
    "compositions" => ( object ) array(
        "title" => "Composition page",
        "icon" => "music"
    ),
);

That will allow accessing them like objects:

$pages->home->title;

or looping through them like this:

for ( $pages as $pageName => $pageObject ) echo $pageName . " has title: " . $pageObject->title;
Sign up to request clarification or add additional context in comments.

Comments

0

I would include the creation as a static member of the class to keep the class specific code together:

class NavItem
{
//  Static member does not require an object to be called
static function create ($def)
{
    $ret = array ();
    foreach ($def as $idx=>$navDef)
        $ret [$idx] = new NavItem ($navDef);
    return $ret;
}
function __construct ($def)
{
    // Do something more specific with the current def (title, icon array)
    $this->param = $def;
}

function display ()
{
    //  Simple example
    echo $this->param ['title'];
    echo $this->param ['icon'];
}
    var                     $param;
};

//  Using your pages array as above
$pages = NavItem::create ($pages);
foreach ($pages as $idx=>$page)
    $page->display ();

1 Comment

That's not what I was trying to accomplish, I gave en example on what I would like ending up with. Having all 3 properties in one itterable object. You missed page attribute that I also need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.