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

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

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

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;
	}
}
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

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

You must use foreach()

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

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 3 Years Ago by happygeek: fixed formatting

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

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 4 Years Ago by xShirowx: n/a

Comments
php.net/json_encode

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 4 Years Ago by mexican_75: typos

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 3 Years Ago by Dani: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.