1

Ok I'm seriously stuck, I've been working on a nav menu that I just cannot get to function how I want it to so i've changed tack but am now stuck again so any help would be much appreciated and desperately needed!

I have the code below and am trying to do the following - I have an array that holds info for all the pages of a site and then another array that holds the ids of the pages that are child pages. What I want to do is use a foreach loop to loop through all the pages of the first array and check whether their ids are in the array of child ids or not. If they are not then they are top level nav pages and I want to output some code and then set up another foreach loop which will check whether any subpages have a parent id of the current page and so on.

I can't seem to work out how to compare $b with the ids in the $childpages array no matter what I try! Is this even possible and if so how please?

This is the first section of what im trying at present

<?php function buildMenu4 ($allpages, $childpages) {

    foreach ($allpages as $pages){

    $a = $pages['parentid'];
    $b = $pages['id'];
    $c = $childpages;


    echo "<ul>\n";



        if (!in_array($b, $c)) {

        DO SOMETHING..........

Array contents of $c:

Array 
( 
[0] => Array ( [id] => 6 ) 
[1] => Array ( [id] => 15 ) 
[2] => Array ( [id] => 100 ) 
[3] => Array ( [id] => 101 ) 
[4] => Array ( [id] => 103 ) 
[5] => Array ( [id] => 104 ) 
[6] => Array ( [id] => 105 ) 
)

edit ---------------------------------

I have reworked my code and am back to a variation of where I was a couple of days ago!! Anyway the code below works as intended until I try to loop it and then it just echoes the results of the first foreach loop e.g. foreach ($allpages as $pages){..... but fails to do anything else.

I am trying to make a function called loopMenu and then run this recursively until there are no more pages to be displayed. I have tried to write the function as shown with the pusedo code in the code block below but I just can't get it to work. I may have muddled up some of the arguments or parameters or perhaps I have just made a big mistake somewhere but I can't see it - any help would be hugely appreciated and desperately needed!

 <?php function buildMenu6 ($allpages, $childpageids, $childpages, $subchildpages) {


        foreach ($childpageids as $childid){
            $c[] = $childid['id'];

            };


            echo "<ul>\n";  

        foreach ($allpages as $pages){


        $a = $pages['parentid'];
        $b = $pages['id'];


            if (!in_array($b, $c)){

                echo "<li><a href=" . $pages['url'] . ">" . $pages['linklabel'] . "</a>";


                    WHERE I WANT THE FUNCTION TO START E.G. function loopMenu($childpages, $subchildpages){...the code that follows....


                    echo"<ul>\n";

                    foreach ($childpages as $childparent) {

                            $d = $childparent['parentid'];
                            $e = $childparent['id'];



                            if (($d == $b) or ($d == $g)) {

                                echo "<li><a href=" . $childparent['url'] . ">" . $childparent['linklabel'] . "</a>";
                                    echo "<ul>\n";

                                    foreach ($subchildpages as $subchild){
                                        $g = $subchild['id'];
                                        $f = $subchild['parentid'];


                                        if ($f == $e){

                                            echo "<li><a href=" . $subchild['url'] . ">" . $subchild['linklabel'] . "</a>";
                        WHERE I TRY TO RERUN THE FUNCTION USING loopMenu($childparent, $subchild);                  
                                            echo "<li/>";
                                            };
                                        };

                                    echo"</ul>\n";
                                echo "</li>";

                        };
                    };
                    echo "</ul>\n";


WHERE I WANT MY FUNCTION TO END E.G. };


echo "</li>";

                };


    };
        echo "</ul>\n";
        }; ?>

Then I call the main buildMenu6 function like so:

<?php buildMenu6($pageids, $childPageIds, $childPageArray, $childPageArray); ?>
Smokescreen
  • 189
  • 3
  • 4
  • 13
  • You probably need to loop over the contents of $c also, but since I can't see how the data in $c (childpages) is structured, I can't write an example. what does the childpages array look like? – Rottingham Mar 17 '14 at 20:56
  • ive edited the post above and added the structure and contents of the array – Smokescreen Mar 17 '14 at 20:59
  • $childpages is an array? – Choudhury A. M. Mar 17 '14 at 21:02
  • 1
    Could you put together an example that runs and shows what isn't working as you expect on http://sandbox.onlinephpfunctions.com/ ? – Justin Mar 17 '14 at 21:05
  • well techincally $childPageArray is the array and I'm passing it into the function using $childpages by calling the function like this - its very possible ive made some silly mistakes along the line as I've been going on this for so long now i've got a bit muddled! – Smokescreen Mar 17 '14 at 21:06

3 Answers3

2

You need a nested foreach (I've changed your var names or used the original ones for readability):

foreach($allpages as $page) {
    foreach($childpages as $child) {
        if($page['id'] == $child['id']) {
            //do something
            break;
        }
    }
}

Or PHP >= 5.5.0 use array_column:

$childids = array_column($childpages, 'id');

foreach($allpages as $page) {
    if(in_array($page['id'], $childids)) {
        //do something
    }
}

As a kind of hybrid:

foreach($childpages as $child) {
    $childids[] = $child['id'];
}
foreach($allpages as $page) {
    if(in_array($page['id'], $childids)) {
        //do something
    }
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Haha I think I had exactly this about 6 hours ago only I had a single = between $id & $child['id'] so I'll give it a go and see if it does what's needed – Smokescreen Mar 17 '14 at 21:15
  • that second one looks very appealing as I think that I will need a recursive loop later in the code and i've been really struggling with that when I've had multiple foreach loops, i just havent been able to get it to work properly – Smokescreen Mar 17 '14 at 21:18
  • i'm still trying to access the $childpages array but can't seem to make it work. for some reason there doesn't seem to be any results for anything like foreach ($childpages as $child){$d = $child['id'];print_r ($d)}; the value of child['id'] just seems to be blank, im wondering if it's because the id key is a second level down in the array e.g. array => key => array => ID => value. would this make any difference? What's really confusing is that the same structure seems to work fine for $a = $pages['parentid'] and this array has the same structure as the child one, what do you think? – Smokescreen Mar 18 '14 at 00:17
  • Given the array you posted, `foreach ($childpages as $child){ echo $child['id']; }` works as its supposed to. – AbraCadaver Mar 18 '14 at 00:36
  • I've updated my code to show what I'm currently working with. It is now working as intended until I try to loop it. I need the looping function to recursively check through sublevels and bring out any sub pages but I can't seem to get it cracked - do you have any suggestions? Many thanks – Smokescreen Mar 18 '14 at 15:18
0

foreach ($allpages as $pages){

$a = $pages['parentid'];
$b = $pages['id'];
$c = $childpages;


echo "<ul>\n";



    // In array isn't aware of the 'id' keys
    $found = false;
    foreach ($c as $id => $value) {
        if ($value == $b) {
             $found = true;
        }
    }

    if ($found) {
        // DO SOMETHING
    }
Rottingham
  • 2,593
  • 1
  • 12
  • 14
  • thanks - I'm just trying this out, I think I might need to rejig it a bit so the last bit is if (!$found){ do something but either way this might be what I'll need. I'll give it a go! – Smokescreen Mar 17 '14 at 21:16
  • I'm still trying to access the contents of the $childpages array and it's not working. I have tried as you suggest with $c as $id => $value but this doesn't seem to return any results - if i print_r ($value) for example it doesn't show anything. Also $c as $value and then $d=$value['id'] for example doesn't work either. Any suggestions? – Smokescreen Mar 18 '14 at 00:06
  • 1
    var_dump($c) after setting it and make sure it actually contains what you think it should – Rottingham Mar 18 '14 at 00:19
  • thanks I've used var_dump to help show what's going on and have now got the code to do what I wanted, except I now need to run a recursive loop on it so it goes back and checks for more subpages and shows them where relevant but I can't get the loop to work. I've updated the code above and was wondering whether you could help? This has been driving me nuts for over a week now! – Smokescreen Mar 18 '14 at 15:20
0

according to THIS SO ANSWER, array_key_exists is (marginally) the fastest array lookup for php.

since, from your description, it seems reasonable to suppose that [id] is a PRIMARY key and, therefore, unique, i would change the [id] dimension and put values directly in its lieu:

$c[105] = NULL; // or include some value, link page URL
... // some code
$needle = 105;
... // some more code
if (array_key_exists($needle,$c)) {

instead of

$c[5] = 105; // $c is the haystack
... // some code
$needle = 105;
... // some more code
foreach ($c as $tempValue) {
    if ($tempValue == $needle) {

in case you want to put values in $c, then you could also use isset (it will return FALSE if array value is NULL for said key).

Community
  • 1
  • 1
tony gil
  • 9,424
  • 6
  • 76
  • 100