I have an array with a few items in it (e.g. item1, item2, item3) and I want to split each item with a "|", so it will look like this at the end: item1|item2|item3.
4 Answers
You must use the implode() function :
$arr = array('item1', 'item2', 'item3');
$str = implode('|', $arr);
var_dump($str);
Will get you :
string 'item1|item2|item3' (length=17)
As a sidenote : what you are doing here is joining (join() is actually an alias of implode()), and not splitting.
Splitting would be the opposite operation, and would be done, in PHP, using the explode() function :
$arr = explode('|', 'item1|item2|item3');
var_dump($arr);
would get you :
array
0 => string 'item1' (length=5)
1 => string 'item2' (length=5)
2 => string 'item3' (length=5)
Comments
That looks like joining and not splitting.
You can use the implode function as:
$str = implode('|',$arr);
Comments
a = ["item1", "item2", "item3" ]
print "|".join( a )
Prints out item1|item2|item3
1 Comment
Pascal MARTIN
This looks a bit like Javascript (or something else ? python ?) -- but this question is tagged
php