12

I Have an array called $pages content is as follows:

Array
(
[01-about-us] => Page Object
    (
        [_uri] => about-us
        [_menuItem] => 01
        [_visable] => 1
    )

[02-contact] => Page Object
    (
        [_uri] => contact
        [_menuItem] => 02
        [_visable] => 1
    )

[_sitemap] => Page Object
    (
        [_uri] => sitemap
        [_menuItem] => 
        [_visable] => 
    )

[home] => Page Object
    (
        [_uri] => home
        [_menuItem] => 
        [_visable] => 1
    )
)

is there an easy way to loop through and get page objects by there properties ie:

<?php foreach($pages->_visible() AS $p): ?>
  <li> page </li>
<?php endforeach ?>
3
  • Do you mean you want to only fetch objects that are visible in this case? Commented Dec 20, 2012 at 9:58
  • 1
    Just loop over the array normally and use an if statement inside the loop to determine whether each page object is visible and a list element should be rendered. Commented Dec 20, 2012 at 9:59
  • Two things: 1: you are inconsistently spelling visible. 2: in the array you refer to visable as a field, in the foreach as a method. Commented Dec 20, 2012 at 10:07

3 Answers 3

23

No. You will have to use an if:

foreach ($pages as $page) {
    if ($page->_visible == 1) {
        echo "<li>page</li>";
    }
}

(Note also you misspelt visible in the array, perhaps a typo?)

Sign up to request clarification or add additional context in comments.

4 Comments

I think the leading underscore (_...) in the output indicates private properties... I did not work a lot with PHP lately though, so I could be wrong.
@FelixKling PHP has a private keyword for that. I couldn't tell if I do not have the class definition of the Page class of OP. He uses it in his own code, so I reckoned this is the way to know if a page is visible. It should probably use a method, though.
perfect thanks it was just a spelling problem i thought i was going crazy.
Ah, apparently private properties are suffixed with :private: codepad.org/6clkYH0u. nvm then :)
6

Or you can utilize PHP's array_filter function:

$pagesVisible = array_filter($pages, function($page) {
    return (bool) $page->_visible;
});

foreach ($pagesVisible as $key => $page) {
    print '<li>' . $key . '</li>';
}

Or shorthand it to:

$filter = function($page) {
    return (bool) $page->_visible;
};
foreach (array_filter($pages, $filter) as $key => $page) {
    print '<li>' . $key . '</li>';
}

Comments

3

You just need to loop through the pages array and inside the loop access the object properties like:

<?php foreach($pages as $k => $p): ?>
   <?php if ($p->_visable === 1): ?>
   <li><?php echo $k; ?></li>
   <?php endif; ?>
<?php endforeach; ?>

Please note that visable is misspelled but thats how it is in your question

3 Comments

Why all the <?php ?> tags?
Because thats how the OP is currently doing it, YES I understand all about BEST PRACTICES but was trying to stick with their current structure ;)
When using PHP as template language, i.e. embedding PHP into HTML, then this style is more readable IMO than anything else. Each statement looks like a "tag" somehow and thus easier to spot / fits better into HTML and the structure / flow is clearer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.