How to convert an array to a string using methods other than JSON?

What is a function in PHP used to convert an array to a string other than using JSON?

I know that there is a function that directly looks like JSON. I just don’t remember.

+50
arrays php string-conversion
Jul 25 '11 at 13:50
source share
9 answers

serialize() is the function you are looking for. It will return a string representation of its input array or object in an internal format specific to PHP. A string can be converted back to its original form using unserialize() .

But be careful that not all objects can be serialized, or some can only be partially serializable and cannot be fully restored using unserialize() .

 $array = array(1,2,3,'foo'); echo serialize($array); // Prints a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:"foo";} 
+71
Jul 25 '11 at 13:50
source share
β€” -

Use the implode() function:

 $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone 
+42
Jul 25 2018-11-11T00:
source share

readable conclusion!

 echo json_encode($array); //outputs---> "name1":"value1", "name2":"value2", ... 

OR

 echo print_r($array, true); 
+17
Apr 24 '15 at 20:21
source share
 <?php $id_nums = array(1,6,12,18,24); $id_nums = implode(", ", $id_nums); $sqlquery = "Select name,email,phone from usertable where user_id IN ($id_nums)"; // $sqlquery becomes "Select name,email,phone from usertable where user_id IN (1,6,12,18,24)" ?>` 
+8
Sep 23 '13 at 17:28
source share

You are looking for serialize () . Here is an example:

 $array = array('foo', 'bar'); //Array to String $string = serialize($array); //String to array $array = unserialize($string); 
+6
Jul 25 2018-11-11T00:
source share

Another good alternative is http_build_query

 $data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); echo http_build_query($data) . "\n"; echo http_build_query($data, '', '&amp;'); 

Will be printed

 foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor 

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

+5
Aug 15 '14 at 2:02
source share

use php implode() or serialize()

+3
Feb 19 '13 at 12:05
source share

Show the array in a beautiful way:

 function arrayDisplay($input) { return implode( ', ', array_map( function ($v, $k) { return sprintf("%s => '%s'", $k, $v); }, $input, array_keys($input) ) ); } $arr = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); echo arrayDisplay($arr); 

Output:

 foo => 'bar', baz => 'boom', cow => 'milk', php => 'hypertext processor' 
+2
Sep 02 '15 at 15:26
source share

There are several ways to do this, some of which have given. implode(), join(), var_export(), print_r(), serialize(), json_encode() exc ... You can also write your own function without them:

Cycle

A For() very useful. You can add the value of your array to the following variable:

 <?php $dizi=array('mother','father','child'); //This is array $sayi=count($dizi); for ($i=0; $i<$sayi; $i++) { $dizin.=("'$dizi[$i]',"); //Now it is string... } echo substr($dizin,0,-1); //Write it like string :D ?> 

In this code, we added the values ​​of $ dizi and a comma to $ dizin:

$dizin.=("'$dizi[$i]',");

Now

 $dizin = 'mother', 'father', 'child', 

This is a string, but it contains an extra comma :)

And then we removed the last comma, substr($dizin, 0, -1);

Output:

'mother', 'father', 'child'

+2
Dec 03 '15 at 23:14
source share



All Articles