0

is there any function that converts the values of an array to string?
i tried with array_values but i dont know how

13
Contributors
12
Replies
38
Views
13 Years
Discussion Span
Last Post by mihai_daniel
Featured Replies
  • 2

    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] Read More

2

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

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
-5

is there any function that converts the values of an array to string?
i tried with array_values but i dont know how

function displayString($arrayText) {
	if (count($alertArr) > 0) {
		$string = '';
		foreach ($arrayTextas $val) {
			$string .= $val."<br />\n";
		}
		return $string;
	} else {
		return false;
	}
}
Votes + Comments
Being a member for such a long time, you should have known how bad it is to bump an old thread.
Look at the damn date of the thread before posting.
6 Year bump to answered thread. Bummer
0

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
?>

0

You must use foreach()

foreach($your_array as $key->$value){
	echo "\$key = ".$key." and \$value = ".$value;
}
0

use print_r

$data = array('foo'=>'bar',
                        'baz'=>'boom',
                        'cow'=>'milk',
                        'php'=>'hypertext processor');
          echo "<pre>";
          echo print_r($data);
          echo "</pre>";

Output :

Array
(
       [foo] => bar
       [baz] => boom
       [cow] => milk
       [php] => hypertext processor
)

Edited by happygeek: fixed formatting

0

i think you find this function helpfull aswell.

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

$data = array('foo'=>'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'hypertext processor');

echo http_build_query($data);

will result in:
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

-2
function arrayToString($array){
	$i = 0;
	$string = '';
	
	foreach ($array as $index => $value ){
		if($i != count($array)-1){
			$string .= "$index :$value, ";
		}else $string .= "$index :$value";
		$i++;
	}
	
	$string = '['.$string.']';
	
	return  $string;
}

this is a basic json vector you'll receive something like this [index: value, index:value] much fun :D

Edited by xShirowx: n/a

Votes + Comments
php.net/json_encode
0

well this is a different approach

$arraystring = print_r($your_array, true);

and if you want to print it somewhere else formated then

$arraystring = '<pre>'.print_r($your_array, true).'</pre>';

or you could mix many arrays and vars if you do this
ob_start();
print_r($var1);
print_r($arr1);
echo "blah blah";
print_r($var2);
print_r($var1);
$your_string_var = ob_get_clean();

cheers

Edited by mexican_75: typos

0

Thanks a lot, Samaru.

Original comment:

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

 $array = array('lastname', 'email', 'phone');
 $comma_separated = implode(",", $array);
 echo $comma_separated; // lastname,email,phone

Edited by Dani: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.