[help need] Array Linking

Started on the old forum in November 2012 · view the original in the archive · old links redirect here

1 reply · 312 views · started 14 years ago · 1 min read

2 participants
asifnayem Developer · 119 posts #1

Well, this thing is confusing me lot. What I need to do is just extracting data from an array and want to put a clickable link with variable. Like this:

Array
 (
     [a] => apple
     [b] => banana
     [c] => orange
  )

to <a href="?data=orange">orange</a>

all I end up is this:

for ($i = 0; $i < count($fruit); ++$i) { print "<a href='?data=".$fruit[$i]."'>".$fruit[$i]."</a>"; }

I want a comma if there is a second fruit out there. But if I put this

foreach ($fruit as $key => $val) {
    print "<a href='?data=$val'>$val</a>, ";
}

it just put the , (comma) after all fruit. Can anyone please tell me how to make it right?

softwarefreak Senior · 349 posts #2
<?
$fruit = array('apple', 'banana', 'orange', 'mango');
$c = count($fruit);
for ($i=0; $i < $c; $i++) { 
	echo $fruit[$i];
	if ($i != $c-1) {
		echo ", ";
	}
	else {
		echo ".";
	}
}

This may give you some ideas

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close