Organizational Research By

Surprising Reserch Topic

convert array to a string using other methods that json using -'php,arrays,string-conversion'


convert array to a string using other methods that json  using -'php,arrays,string-conversion'

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

I know there is a function that directly does like JSON. I just don't remember.
    

asked Sep 8, 2015 by rajesh
0 votes
7 views



Related Hot Questions

6 Answers

0 votes
serialize() is the function you are looking for. It will return a string representation of its input array or object in a PHP-specific internal format. The string may be converted back to its original form with unserialize(). But beware, that not all objects are serializable, or some may be only partially serializable and unable to be completely restored with 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";}
answered Sep 8, 2015 by rajesh
0 votes
Use the implode() function: $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone
answered Sep 8, 2015 by rajesh
0 votes
<?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)" ?>`
answered Sep 8, 2015 by rajesh
0 votes
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);
answered Sep 8, 2015 by rajesh
0 votes
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 print foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor More info here http://php.net/manual/en/function.http-build-query.php
answered Sep 8, 2015 by rajesh
0 votes
Display array in 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); Displays: foo => 'bar', baz => 'boom', cow => 'milk', php => 'hypertext processor'
answered Sep 8, 2015 by rajesh

...