0

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.

1

4 Answers 4

3

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)
Sign up to request clarification or add additional context in comments.

Comments

1

you can use implode('|', $array);

Comments

1

That looks like joining and not splitting.

You can use the implode function as:

$str = implode('|',$arr);

Comments

0
a = ["item1", "item2", "item3" ]

print "|".join( a )

Prints out item1|item2|item3

1 Comment

This looks a bit like Javascript (or something else ? python ?) -- but this question is tagged php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.