Jump to content

array_merge leaves empty array. What's wrong?


Pascal
 Share

Recommended Posts

Hi, I am a complete beginner in coding PHP and stumbled upon this problem. This is the code:

<?php
  $contentpages = $pages->get("/content/")->children;
  unset($contentpages[0]);
  echo $contentpages;
  $footersections = $pages->get("/footer/")->children;
  echo $footersections;
  $navlinks = array_merge($contentpages, $footersections);
  echo $navlinks;
  foreach($navlinks as $link) {
  $class = $link === $page->rootParent ? " class='on'" : '';
  echo "<div class='nav-button'><a$class href='{$link->url}'>{$link->title}</a></div>";
  }
?>

So first I get the children of the content page and exclude the first one, which is the homepage. The first echo spits out an array of two values. Then I repeat the procedure for sections in the footer which I want to link to. The second echo shows me those values. But then, after using array_merge to merge(?) the arrays, the resulting variable is empty. I suppose I made a really stupid mistake somewere along the way?

By the way, let me know if anything more elegant comes to your mind, I am eager to learn.

Link to comment
Share on other sites

You can also use an import method of PageArray. Note that the page array of PW isn't a regular php array, that's why it doesn't work.

you could also do something like this.

$pa = new PageArray();
$pa->import( $pages->get("/content/")->children() );
$pa->import( $pages->get("/footer/")->children() );

foreach($pa as $p){
...
}

You may check out the cheatsheet on the API pages, and look for the PageArray/WireArray sections. There's lots of useful methods.

For example instead of this:

$contentpages = $pages->get("/content/")->children;
  unset($contentpages[0]);

do this to slice off the first entry in the resulting page array:

$contenpages = $page->get("/content/")->children()->slice(0);
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...