Questions tagged [implode]

implode() is a PHP function that joins together an array (optionally based on a 'glue' (inverse delimiter)) and returning a string.

implode() is a PHP function that joins together an array (optionally based on a 'glue' (inverse delimiter - defaults to an empty string) and returning a string.

string implode ( string $glue , array $pieces )
string implode ( array $pieces )

Example

$array = array("a", "b", "c", "d");
print_r(implode(" ", $array));
print_r(implode($array));

returns:

a b c d
abcd
749 questions
363
votes
13 answers

Convert flat array to a delimited string to be saved in the database

What is the best method for converting a PHP array into a string? I have the variable $type which is an array of types. $type = $_POST[type]; I want to store it as a single string in my database with each entry separated by |…
Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
314
votes
7 answers

Implode an array with JavaScript?

Can I implode an array in jQuery like in PHP?
Omega
  • 8,640
  • 9
  • 30
  • 29
154
votes
16 answers

php implode (101) with quotes

Imploding a simple array would look like this $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); and that would return this lastname,email,phone great, so i might do this instead $array = array('lastname',…
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
142
votes
14 answers

How to implode array with key and value without foreach in PHP

Without foreach, how can I turn an array like this array("item1"=>"object1", "item2"=>"object2",......."item-n"=>"object-n"); to a string like this item1='object1', item2='object2',.... item-n='object-n' I thought about implode() already, but it…
tom91136
  • 8,662
  • 12
  • 58
  • 74
135
votes
11 answers

Fastest way to implode an associative array with keys

I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use '&' for xhtml links or '&' otherwise. My first inclination is to use foreach…
matpie
  • 17,033
  • 9
  • 61
  • 82
133
votes
2 answers

Python equivalent for PHP's implode?

Is there an equivalent for PHP's implode in Python? I've read in and split up a set of delimited words, and now I want to sort them out in random orders and print the words out with spaces in between. implode — Join array elements with a…
pythonian29033
  • 5,148
  • 5
  • 31
  • 56
124
votes
22 answers

How to implode a vector of strings into a string (the elegant way)

I'm looking for the most elegant way to implode a vector of strings into a string. Below is the solution I'm using now: static std::string& implode(const std::vector& elems, char delim, std::string& s) { for…
ezpresso
  • 7,896
  • 13
  • 62
  • 94
116
votes
9 answers

How can I implode an array while skipping empty array items?

Perl's join() ignores (skips) empty array values; PHP's implode() does not appear to. Suppose I have an array: $array = array('one', '', '', 'four', '', 'six'); implode('-', $array); yields: one---four--six instead of (IMHO the…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
91
votes
8 answers

Add a prefix to each item of a PHP array

I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated. Essentially I…
MBL
  • 1,197
  • 2
  • 11
  • 17
87
votes
7 answers

Return single column from a multi-dimensional array

I'm a novice at PHP and I need a quick solution to the following problem but can't seem to come up with one: I have a multi-dimensional array like so Array ( [0] => Array ( [blogTags_id] => 1 [tag_name] => google …
CobaltBabyBear
  • 2,188
  • 6
  • 26
  • 47
78
votes
7 answers

PHP Implode But Wrap Each Element In Quotes

Assume I have an array: $elements = array('foo', 'bar', 'tar', 'dar'); Then I want to build up a DELETE IN SQL query: $SQL = "DELETE FROM elements WHERE id IN ('" . implode(',', $elements) . "')"; The problem is that the ids in…
Justin
  • 42,716
  • 77
  • 201
  • 296
48
votes
3 answers

Alternative of php's explode/implode-functions in c#

are there a similar functions to explode/implode in the .net-framework? or do i have to code it by myself?
Sheldon Cooper
  • 617
  • 1
  • 5
  • 9
43
votes
4 answers

Go equivalent of PHP's 'implode'

What is the Go equivalent of PHP's 'implode'?
code_martial
  • 1,185
  • 1
  • 11
  • 23
33
votes
3 answers

PHP Implode wrap in tags

Been trying to google an answer but cant seem to find anything, I have the following...
Liam
  • 9,725
  • 39
  • 111
  • 209
33
votes
7 answers

Array to string conversion error when using implode

I'm confused about an error I am getting stating Array to string conversion The reason I'm confused is I'm trying to do exactly that, convert an array to a string, using implode which according to the manual should allow me to convert my array into…
Francesca
  • 26,842
  • 28
  • 90
  • 153
1
2 3
49 50