For an array like below; what would be the best way to get the array values and store them in a variable as a string?

Array ( [0] => 33160 [1] => 33280 [2] => 33180 [3] => 33163 [4] => 33181 [5] => 33164 [6] => 33162 [7] => 33179 [8] => 33154 [9] => 33008 [10] => 33009 [11] => 33161 [12] => 33261 [13] => 33269 [14] => 33169 [15] => 33022 [16] => 33141 [17] => 33168 [18] => 33020 [19] => 33023 [20] => 33019 [21] => 33153 [22] => 33238 [23] => 33138 [24] => 33167 [25] => 33082 ) 
share|improve this question
2  
Why does this need to be a string? How would you like this string to be formatted? What are you doing with it? More details would be nice. – Rocket Hazmat Nov 23 '10 at 20:34
just comma separated zipcodes like so 33160,33280,33180 etc... – user74283 Nov 23 '10 at 20:45

6 Answers

up vote 1 down vote accepted

I would turn it into CSV form, like so:

$string_version = implode(',', $original_array)

You can turn it back by doing:

$destination_array = explode(',', $string_version)
share|improve this answer
CSV is the format user74283's specifies in his comment on his question. – Isaac Sutherland Nov 23 '10 at 22:03

I would turn it into a json object:

 $stringRepresentation= json_encode($arr);
share|improve this answer

serialize() and unserialize() convert between php objects and a string representation.

share|improve this answer

implode?

share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ioannis Karadimas Aug 18 '12 at 6:28
implode(' ',$array);
share|improve this answer

Using implode(), you can turn the array into a string.

$str = implode(',', $array); // 33160,33280,33180,...
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.