dhrm
dhrm

Reputation: 14934

PHP: How to convert array to string

I've a PHP server with a log table in my MySQL database. When users connect to my server, I would like to store all the $_POST and $_GET variables into the log entry. The two variables is an array. Is there an easy way in PHP to convert these arrays to a string representation suitable for storing in my mySQL database?

As far as I know implode() is just for one-dimensional arrays. Maybe json_encode() would be a good way to go?

Upvotes: 1

Views: 686

Answers (3)

konsolenfreddy
konsolenfreddy

Reputation: 9671

You can use serialize()/unserialize()

see PHP.net

Upvotes: 2

Amber
Amber

Reputation: 526763

json_encode() would work fine and be fairly compact, thus saving you space over some other more verbose alternatives.

Upvotes: 4

Francois
Francois

Reputation: 10978

you can try with print_r($myTab).

Example (http://php.net/manual/fr/function.print-r.php):

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>

gives

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

Upvotes: 0

Related Questions