1
$menu = array(
    0 =>'top', 
    1 =>'photography', 
    2 =>'about'
);

<?php
function main_menu ($menu) {
    $return = '<div class="menu_entry">' . PHP_EOL .'';
        foreach( $menu as $key => $value)
        {
        $return .= '<a class="menu" href="index.php#' . $menu[$key] . '">' . $menu[$key] . '</a>' . PHP_EOL .'';
    }
  $return .= '</div>';
return $return;
}
?>

 <?php echo main_menu($menu[1]); ?>

What i basically want to do is to pass a specific array value when i'm echoing out the menu. I'm building a single page website with anchors and i want to pass value's so i can echo out the "top"-link.

I'm stuck at the point on how to pass the $key value trough the function.

**edit: I'm trying to print specific links. I want a function that is able to print out an link but i want to specify the link to print via the function argument.

for example:

 <?php echo main_menu($key = '0'); ?>
 result:
 prints url: top

 <?php echo main_menu($key = '2'); ?>
 result:
 prints url: photography

**

(A lack of jargon makes it a bit harder to explain and even harder to google. I got my books in front of me but this is taking a lot more time than it should.)

2
  • 3
    It's not clear what you're trying to accomplish. Could you clarify more on "I'm stuck at the point on how to pass the $key value trough the function."? Commented Mar 7, 2014 at 16:39
  • Are you trying to print the menu on the specified index? Commented Mar 7, 2014 at 16:42

3 Answers 3

1

You either need to pass the entire array and loop, or pass a single array item and not loop:

Single Item:

function main_menu ($menu) {
    $return  = '<div class="menu_entry">' . PHP_EOL .'';
    $return .= '<a class="menu" href="index.php#' . $menu . '">' . $menu . '</a>' . PHP_EOL .'';
    $return .= '</div>';

    return $return;
}

echo main_menu($menu[1]);

Entire Array:

function main_menu ($menu) {
    $return = '<div class="menu_entry">' . PHP_EOL .'';

    foreach($menu as $value) {
        $return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
    }
    $return .= '</div>';

    return $return;
}

echo main_menu($menu);

You don't need $menu[$key] just use the $value.

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

1 Comment

Aah, yes ofcourse! I had it a for loop first where i used $menu[$i], i guess i didn't think it trough when i changed it. Your first solution was what i was looking for, thanks!
0

Should you not just be using $value inside your loop? And passing the entire array rather than one item of the $menu array?

$menu = array(
    0 =>'top', 
    1 =>'photography', 
    2 =>'about'
);

<?php
function main_menu ($menu) {
    $return = '<div class="menu_entry">' . PHP_EOL .'';
        foreach( $menu as $key => $value)
        {
        $return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
    }
  $return .= '</div>';
return $return;
}
?>

<?php echo main_menu($menu); ?>

Comments

0

Try:

echo main_menu($menu); // You will get your links printed

Instead of

echo main_menu($menu[1]); // In this case error is occured like : **Invalid argument supplied for foreach**

NOTE: You can use $value instead of $menu[$key]

3 Comments

If echo out $menu it shows all entries. I want to pick out specific entries to print.
@Rico If you want to make specific links than you have to parse them in loop, update your question and I'll help you
@Rico I meant, explain what do you mean in specific links ? show me desirable output

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.