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

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

3 Answers 3

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

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

echo $ids;  // => #id1,#id2,...
Sign up to request clarification or add additional context in comments.

6 Comments

Love it... so short, but exactly on target. +1
@Doug: Make it even shorter: '#' . join(',#', $arr);.
@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.
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: I always use implode() too, I was just adding an emphasis on Doug comment. =)
|
2
$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 . " ";
}

3 Comments

+1 for covering both possible interpretations of an ambiguous question.
Yeah, read it wrong the first time. Thought he was asking how to access them period.
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.
1

you can iterate the array

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

each "$k" is your array items

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.