-2

I have an array having various numbers:

$array = [1,2,3,4];

I would like to have some code that will extract those values and prepend 'ad' to them while imploding into an html attribute:

<div class="ad1 ad2 ad3"> 

How can I do that?

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
urok93
  • 537
  • 3
  • 7
  • 25
  • +1 don't know why ppl are downvoting this – Simon Feb 06 '13 at 14:23
  • @Simon Simply because drtanz didn't post what they have and haven't tried. – Colin M Feb 06 '13 at 14:27
  • Still not worth a downvote, just ask for it like hakre did... http://stackoverflow.com/privileges/vote-down – Simon Feb 06 '13 at 14:29
  • 1
    @Simon I personally didn't downvote, but I can see why people would file this as "no-effort-expended" - the effort comes with the proof of trial. Takes no effort to ask a question - it takes effort to try to solve it on your own. – Colin M Feb 06 '13 at 14:31

4 Answers4

4

Combine implode() with array_map() to alter the values prior to imploding them.

Something like this:

$outputString = implode(' ',array_map(function($val) {return "ad{$val}";}, $inputArray))
SDC
  • 14,192
  • 2
  • 35
  • 48
  • Hah, right as I was answering with the same thing. +1 to you. – Colin M Feb 06 '13 at 14:23
  • This is the cleanest method. – Daniel Dinu Feb 06 '13 at 14:24
  • This isn't the cleanest this is overkill - All `array_map` is doing is looping internally to generate yet another array. Mathieu Imbert's answer is identical, and more efficient with an explicit loop doing the same thing, not to mention that not all hosts support PHP versions with anonymous functions. Just because you can come up with a one-liner, doesn't mean you *should*. – nickb Feb 06 '13 at 14:27
  • @nickb If you want to get technical, this is actually slower than Mathieu Imbert's solution for two reasons: 1) It loops the array twice (`implode` and `array_map`) and 2) it has the overhead of a function call for each item. With that being said, I think it's a stylistic choice since I doubt this list would be big enough to make a difference. – Colin M Feb 06 '13 at 14:30
  • Of more concern to me would be that not all hosts support this, else it's great. – urok93 Feb 06 '13 at 14:40
  • 1
    I would certainly be concerned about using a host that didn't support anon functions, since that would mean they're on a version of PHP that has been completely unsupported for at least two years. – SDC Feb 06 '13 at 14:42
2

Loop over your array, and do whatever you have to:

foreach ($array as $item) {
   ...
}

In your exemple:

$className = "";
foreach ($array as $item) {
  $className .= "ad".$item." ";
}

echo '<div class="'.$className.'">';
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • @Dukeling I didn't want to do all the work, but ok. Edited. – Tchoupi Feb 06 '13 at 14:20
  • I'm all for just pointing someone in the right direction, rather than doing their work for them, but in this particular case I feel your pre-edit answer was a bit too vague to be helpful. – Bernhard Barker Feb 06 '13 at 14:36
2

You can either loop over it as Mathieu suggests, or do an implode statement:

$array = array(1,2,3,4);
if (sizeof($array)>0) {
  $class = 'ad'.implode(' ad',$array);
}
echo $class; // ad1 ad2 ad3 ad4
Nick
  • 6,316
  • 2
  • 29
  • 47
-1

Try this:

echo implode(' ad', $array);
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55