943,787 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 347646
  • PHP RSS
Jul 16th, 2004
0

Array to String

Expand Post »
is there any function that converts the values of an array to string?
i tried with array_values but i dont know how
Reputation Points: 10
Solved Threads: 0
Newbie Poster
janzaldo is offline Offline
3 posts
since Jul 2004
Jul 17th, 2004
2

Re: Array to String

For a one dimensional array, use the implode() function:


[php]
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
[/php]
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jan 20th, 2010
-5
Re: Array to String
Click to Expand / Collapse  Quote originally posted by janzaldo ...
is there any function that converts the values of an array to string?
i tried with array_values but i dont know how
PHP Syntax (Toggle Plain Text)
  1. function displayString($arrayText) {
  2. if (count($alertArr) > 0) {
  3. $string = '';
  4. foreach ($arrayTextas $val) {
  5. $string .= $val."<br />\n";
  6. }
  7. return $string;
  8. } else {
  9. return false;
  10. }
  11. }
Reputation Points: 5
Solved Threads: 0
Light Poster
ablitz is offline Offline
48 posts
since Jul 2009
Jan 20th, 2010
0
Re: Array to String
yezzz use PHP function implode()
Reputation Points: 32
Solved Threads: 63
Posting Pro in Training
vaultdweller123 is offline Offline
496 posts
since Sep 2009
Feb 18th, 2010
0
Re: Array to String
Implode function does the trick for me. Here an example that I followed:
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
?>
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Sep 19th, 2010
0
Re: Array to String
You must use foreach()

PHP Syntax (Toggle Plain Text)
  1. foreach($your_array as $key->$value){
  2. echo "\$key = ".$key." and \$value = ".$value;
  3. }
Reputation Points: 13
Solved Threads: 28
Posting Whiz in Training
P0lT10n is offline Offline
227 posts
since Apr 2010
Oct 17th, 2010
0
Re: Array to String
i think you find this function helpfull aswell.

http://php.net/manual/en/function.http-build-query.php

php Syntax (Toggle Plain Text)
  1. $data = array('foo'=>'bar',
  2. 'baz'=>'boom',
  3. 'cow'=>'milk',
  4. 'php'=>'hypertext processor');
  5.  
  6. echo http_build_query($data);
  7.  
  8. will result in:
  9. foo=bar&baz=boom&cow=milk&php=hypertext+processor
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ludohelder is offline Offline
1 posts
since Oct 2010
Aug 17th, 2011
0

Not a string but simple and readable

use print_r

php Syntax (Toggle Plain Text)
  1. $data = array('foo'=>'bar',
  2. 'baz'=>'boom',
  3. 'cow'=>'milk',
  4. 'php'=>'hypertext processor');
  5. echo "<pre>";
  6. echo print_r($data);
  7. echo "</pre>";
  8.  
  9. Output :
  10.  
  11. Array
  12. (
  13. [foo] => bar
  14. [baz] => boom
  15. [cow] => milk
  16. [php] => hypertext processor
  17. )
Last edited by jem215; Aug 17th, 2011 at 10:14 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jem215 is offline Offline
1 posts
since Aug 2011
Nov 9th, 2011
0
Re: Array to String
Click to Expand / Collapse  Quote originally posted by ludohelder ...
i think you find this function helpfull aswell.

http://php.net/manual/en/function.http-build-query.php

php Syntax (Toggle Plain Text)
  1. $data = array('foo'=>'bar',
  2. 'baz'=>'boom',
  3. 'cow'=>'milk',
  4. 'php'=>'hypertext processor');
  5.  
  6. echo http_build_query($data);
  7.  
  8. will result in:
  9. foo=bar&baz=boom&cow=milk&php=hypertext+processor

Thanks your answer
Do u have any function convert string to array exclude explode function of php?
Thanks, and wait your response
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanclip is offline Offline
1 posts
since Jul 2009
Nov 9th, 2011
0
Re: Array to String
See split() .

Next time please start a new thread.
Last edited by pritaeas; Nov 9th, 2011 at 7:25 am.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,172 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Insert a URL instead of existing code
Next Thread in PHP Forum Timeline: Invalid Arguments passed implode





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC