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

PhpArraysString Conversion

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

Use the implode() function:

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

Solution 3 - Php

readable output!

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

OR

echo print_r($array, true);

Solution 4 - Php

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

$array = array('foo', 'bar');

//Array to String
$string = serialize($array);

//String to array
$array = unserialize($string);

[1]: http://php.net/manual/function.serialize.php "serialize()"

Solution 5 - Php

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, '', '&');

Will print

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

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

Solution 6 - Php

use php implode() or serialize()

Solution 7 - Php

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'

Solution 8 - Php

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

A For() loop is very useful. You can add your array's value to another variable like this:

<?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 $dizi's values and comma to $dizin:

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

Now

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

It's a string, but it has an extra comma :)

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

Output:

> 'mother','father','child'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaniclornView Question on Stackoverflow
Solution 1 - PhpMichael BerkowskiView Answer on Stackoverflow
Solution 2 - PhpTjekklesView Answer on Stackoverflow
Solution 3 - PhpT.ToduaView Answer on Stackoverflow
Solution 4 - Phpuser562854View Answer on Stackoverflow
Solution 5 - PhpcasivaagustinView Answer on Stackoverflow
Solution 6 - Phpumesh unnikrishnanView Answer on Stackoverflow
Solution 7 - PhpSomnath MulukView Answer on Stackoverflow
Solution 8 - PhpBARIS KURTView Answer on Stackoverflow