PHP Nested array to govern a navigation function won't work

Can someone tell me why this isn't working?

The "isTrue" => true is actually a function that determines if the the content for that field exists or not, I've var_dumped it and it works.

I'm trying to output one (or all, depends on "isTrue" => true) of the navigation links as this:

<li><a class="gallery-tab" href="#gallery-tab">gallery</a></li>

as a content type specific navigation element. I had this working before in a much cruder way but I'm trying to make my code more maintainable and this has been one frustrating road block for me.

When I run the code below, I get NULL

While I'm pretty comfortable with PHP, I'm not a seasoned vet of it. Usually when I show a problem like this to you guys, someone sees the problems pretty quickly and I've just been banging my head against the wall trying to figure it out.

Here is the code in question:

<?php

$navArray[0] = array(
    "title" => "statement",
    "class" => "statement-tab",
    "isTrue" => true
);
$navArray[1] = array(
    "title" => "gallery",
    "class" => "gallery-tab",
    "isTrue" => true
);
$navArray[2] = array(
    "title" => "video",
    "class" => "video-tab",
    "isTrue" => true
);
$navArray[3] = array(
    "title" => "poetry",
    "class" => "poetry-tab",
    "isTrue" => true
);

function get_nav() {
    foreach ($navArray as $array ) {
        if ($array["isTrue"] == true) {

            $output = "";
            $output = $output . "<li>";
            $output = $output . '<a class="' . $array["class"] . '" href="#' . $array["class"] . '" />';
            $output = $output . $array["title"];
            $output = $output . "</a>";
            $output = $output . "</li>";

            return $output;

        }
    }
}

$getNav = get_nav();

 ?>

<pre><?php var_dump($getNav); ?></pre>

If anything is unclear please let me know and I'll do my best to clarify.

EDIT: zeantsoi kindly pointed out that I was calling in the wrong array. I fixed and tested it and still no luck.

Answers


I can see a couple of problems. Firstly, $nav_array doesn't exist. Even assuming it should be $navArray, it still doesn't exist in the scope of the function. I'd suggest something like this:

function get_nav($navArray) {
    foreach($navArray as $array) {
...
$getNav = get_nav($navArray);

The second issue is that you are only ever going to get the first nav item, because you're compiling and returning the output inside the loop. So you need to update it to be something like this:

function get_nav($navArray) {
    $output = "";
    foreach($navArray as $array) {
        if ($array["isTrue"] == true) {
            $output .= "<li>";
            $output .= '<a class="' . $array["class"] . '" href="#' . $array["class"] . '" />';
            $output .= $array["title"];
            $output .= "</a>";
            $output .= "</li>";
        }
    }
    return $output;
}

There is three things you must change in your code:

1 - call for $navArray instead of $nav_array.

2 - $output = ""; must be right before the foreach loop.

3 - the return $output; must be in the end of your get_nav() function, if you intent to get the output of your whole $navArray.


Need Your Help

Internet Explorer or any Browser F1 keypress displays your own help

javascript internet-explorer

I would like to display the user selected help file when pressing F1. This should work on every browser where I test my application. How can I stop the default help file from being displayed?

Tomcat 6.0.28 Unresponsive Because of Blocked Threads During Soak Test

java multithreading tomcat garbage-collection deadlock

We ran several soak tests for our new version of a Java EE web application running on Tomcat 6.0.28 and OpenJDK Runtime Environment (IcedTea6 1.11.11). The problem occurs in the web tier.