37

I want to run a for loop through an array and create anchor elements for each element in the array, where the key is the text part and the value is the URL.

How can I do this please?

Thank you.

2

4 Answers 4

63

This should do it

foreach($yourArray as $key => $value) {
    //do something with your $key and $value;
    echo '<a href="' . $value . '">' . $key . '</a>';
}

Edit: As per Capsule's comment - changed to single quotes.

5
  • 1
    It's another 13 minutes until I can set it as the correct answer, but I will. Commented Apr 21, 2011 at 14:38
  • 2
    If you want to edit the value in the loop, change $value to &$value. Then, any changes you make to $value in the loop will affect the original array. For more information, see PHP's page on references: php.net/manual/en/language.references.php Commented Apr 21, 2011 at 14:39
  • 1
    -1 for using single quotes in HTML code. Would break validation with some doctypes (like XHTML) Commented Apr 21, 2011 at 14:43
  • @Capsule - I'll be honest I didn't even know that was the case. Coming from ASP.NET background where you simply assume the page won't validate, so I never bothered to check things like that. I ended up using the above syntax out of sheer convenience in PHP Commented Apr 21, 2011 at 14:45
  • @Marek btw PHP is faster when using single quotes because it doesn't try to interpret any variables or special chars inside in the string. So you should definitely use that ;-) I would also concatenate . "\n" for readability purposes when viewing HTML source code, even if I definitely don't recommend using echo in the middle of a PHP script. Commented Apr 21, 2011 at 14:49
30

For some specific purposes you may want to know the current key of your array without going on a loop. In this case you could do the following:

reset($array);
echo key($array) . ' = ' . current($array);

The above example will show the Key and the Value of the first record of your Array.

The following functions are not very well known but can be pretty useful in very specific cases:

key($array);     //Returns current key
reset($array);   //Moves array pointer to first record
current($array); //Returns current value
next($array);    //Moves array pointer to next record and returns its value
prev($array);    //Moves array pointer to previous record and returns its value
end($array);     //Moves array pointer to last record and returns its value
1
  • I think this is the way SPL does it. Thanks. Commented May 22, 2012 at 19:10
2

Like this:

$array = array(
    'Google' => 'http://google.com',
    'Facebook' => 'http://facebook.com'
);

foreach($array as $title => $url){
    echo '<a href="' . $url . '">' . $title . '</a>';
}
1

In a template context, it would be:

<?php foreach($array as $text => $url): ?>
    <a href="<?php echo $url; ?>"><?php echo $text; ?></a>
<?php endforeach; ?>

You shouldn't write your HTML code inside your PHP code, hence avoid echoing a bunch of HTML.

This is not filtering anything, I hope your array is clean ;-)

0

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.