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

Recommended Answers

All 12 Replies

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;
	}
}
commented: 6 Year bump to answered thread. Bummer -1
commented: Look at the damn date of the thread before posting. -1
commented: Being a member for such a long time, you should have known how bad it is to bump an old thread. -1

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

Member Avatar for P0lT10n

You must use foreach()

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

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

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
)

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

See split() .

Next time please start a new thread.

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

commented: php.net/json_encode -3

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.