tags:

views:

71

answers:

3
$arr = array('id1','id2',...);

How to get #id1,#id2,.. from the above array?

+2  A: 
$arr[0];
$arr[1];

The code you just provided is the same as

$arr = array(
    0 => "id1",
    1 => "id2");

In order to make a list of ids in CSS

$string = '';
foreach($arr as $id)
{
    $string .= "#" . $id . " ";
}
Chacha102
+1 for covering both possible interpretations of an ambiguous question.
zombat
Yeah, read it wrong the first time. Thought he was asking how to access them period.
Chacha102
Just because he used id1 and id2 doesn't mean every ID will begin with id and end with a number, those are just examples. They could be IDs such as 'harp1', 'mem1', and 'gut2', completely irrelevant of each other.
animuson
+4  A: 
$arr = array('id1', 'id2', ...);

$ids = '#' . join(',#', $arr);

echo $ids;  // => #id1,#id2,...
Jordan
Love it... so short, but exactly on target. +1
Doug Neiner
@Doug: Make it even shorter: `'#' . join(',#', $arr);`.
Alix Axel
@Alix True. I always use `implode` though because it parallels `explode`. Since `split` is deprecated, it seems weird to me to use `join` and `explode` together.
Doug Neiner
Oh, fair enough. For some reason I thought that since 'split' is deprecated then so is 'join,' but I that was silly of me. I've updated my answer, thanks.
Jordan
@Jordan: I always use `implode()` too, I was just adding an emphasis on Doug comment. =)
Alix Axel
Right, but I prefer join myself. It just makes more sense in my brain. "implode" seems like such a violent verb. I guess my approach to programming is a bit synaesthetic at times.
Jordan
+1  A: 

you can iterate the array

foreach ($arr as $k){
  print "#".$k."\n";
}

each "$k" is your array items

ghostdog74