0

I have an array saved as a variable

$custgalarr=explode(",",$custgallinks);

If I print this like so, it shows up like this

$closeLink='</a>'.print_r($custgalarr);
Array ( [0] => cat [1] => dog [2] => moose ) 

I want to be able to have:

$closeLink='</a><a href="cat"><a href="dog"><a href="moose">'

But I can not get a foreach to work. How would I set $closeLink to dynamically create the for each item that is in the array?

Thanks for the help

1
  • What code did you try with foreach? Commented Jul 20, 2011 at 17:30

4 Answers 4

2
$closeLink = '</a>'.'<a href="'.implode('"><a href="', $custgalarr).'">';

Should do it.

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

2 Comments

is there a reason this would create a duplicate in safari?
my apologies, I was missing a closing tag, safari issue fixed. Thanks, this worked out perfectly
1
function arrayWalk(&$item){

return "<a href="$item"></a>";

}

$closeLink = array_walk( $closeLink , "arrayWalk");
echo "</a>" . impload("",$closeLink);

OR

$closeLink = array_walk( $closeLink , create_function("&$item",'return "<a href="$item"></a>";'));
echo "</a>" . impload("",$closeLink);

Comments

0
foreach ($custgalarr as $item) {
  echo '<a href="', htmlspecialchars($item), '">', htmlspecialchars($item), '</a>';
}

Comments

0

simple

$closelink = "</a>";

foreach($custgalarr as $item => $val){
        $closelink .= "<a href=\"{$val}\">";
}

Comments

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.