2

im trying to create a tags from array items and the number of array items is always different,

$myarray = 'sports,politics,entertainment,celebs';
$siteurl = 'http://example.com/';
$tag = explode(',', $myarray);

this is what i do

echo '<p>tag : <a href="'.$siteurl.'?'.$tag[0].'" >'.$tag[0].'</a>, 



<a href="'.$siteurl.'?'.$tag[1].'" >'.$tag[1].'</a>, 
<a href="'.$siteurl.'?'.$tag[2].'" >'.$tag[2].'</a>, 
<a href="'.$siteurl.'?'.$tag[3].'" >'.$tag[3].'</a>,
<a href="'.$siteurl.'?'.$tag[4].'" >'.$tag[4].'</a></p>';

how can i echo this tags with a single call and get all array items no mather how much the items number?

Edit : typo for $vatag

3
  • Use a foreach loop, or a for loop Commented Oct 16, 2018 at 20:04
  • 1
    From where does this $vatag came? Commented Oct 16, 2018 at 20:05
  • 1
    you could try explode like this: $tag = explode('<a href="' . $myarray->siteurl . '?' . $myarray->tag[1]. '" >' . $myarray->vatag[1] . '</a>', $myarray); Commented Oct 16, 2018 at 20:07

4 Answers 4

5

You can use a foreach loop to accomplish this, as follows:

$myarray = 'sports,politics,entertainment,celebs';
$siteurl = 'http://example.com/';
$tag = explode(',', $myarray);

foreach($tag as &$value) {
    echo '<a href="'.$siteurl.'?'.$value.'" >'.$value.'</a>';
}

The result:

<a href="http://example.com/?sports" >sports</a><a href="http://example.com/?politics" >politics</a><a href="http://example.com/?entertainment" >entertainment</a><a href="http://example.com/?celebs" >celebs</a>
Sign up to request clarification or add additional context in comments.

3 Comments

just an observation, no need to pass by reference in the loop
@ArtisticPhoenix sorry its a typo
@ArtisticPhoenix I don't see a second array. It was a typo and the question is now corrected by the user I think.
1

Then you can do them like this:

$len = count($tag);
for($i=0;$i<$len;$i++){
   echo '<a href="'.$siteurl.'?'.$tag[$i].'" >'.$tag[$i].'</a>';
}

So the full code

$myarray = 'sports,politics,entertainment,celebs';
$siteurl = 'http://example.com/';
$tag = explode(',', $myarray);

$len = count($tag);
for($i=0;$i<$len;$i++){
   echo '<a href="'.$siteurl.'?'.$tag[$i].'" >'.$tag[$i].'</a>';
}

Another, slightly more advanced way is like this (which is what I typically do)

$myarray = 'sports,politics,entertainment,celebs';
$siteurl = 'http://example.com/';
$tag = explode(',', $myarray);

$html = array_map(function($item)use($siteurl){
    return '<a href="'.$siteurl.'?'.$item.'" >'.$item.'</a>';
}, $tag);

echo implode("\n",$html);

That way in the source code for the page each link is on a new line, which makes it a bit easier to read the source. You can implode with "", for no line return too.

Cheers!

2 Comments

I am not sure I know what you mean?. I do post some long answers though.... If that is what you mean, sorry, I just try to explain things as best I can. At the moment my wrists really hurt, so .... I been slammed updating mysql_ and other pre 7 stuff, I remember when it changed from 4 to 5, all those constructors and other things...
Sorry, should have been more explicit. meant to say you'd dropped the original paragraphs and commas from the html output. Shame there isn't something like python 2 to 3! Poor you.
1

What you want is a foreach loop, which will run through event element of the array, regardless of its length.

// Start with initialising an empty string
$str = '';

// Loop through every element of the $tag array,
//   using $value to hold the value of the current element in the loop
foreach ($tag as $value) {
  // Append the new link to the end of the string
  $str .= '<a href="' . $siteurl . '?' . $value . '" >' . $value . '</a>' . ', ';
}

// Echo the final array, after trimming off any spaces or commas
//   from the end
echo '<p>tag :' . rtrim($str, ', ') . '</p>';

Comments

0

You don't have a $vatag array, or do you? You can't match something that you don't have or if you have it show us its' contents.

Simply use this code:

$siteurl = 'http://example.com/';
$tags = explode(',', 'sports,politics,entertainment,celebs');
echo '<p>Tags: ';
foreach($tags as $tag) {
    echo "<a href=\"{$siteurl}?{$tag}\">{$tag}&nbsp;</a>";
}
echo '</p>';

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.