1

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?

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can use serialize()/unserialize()

see PHP.net

2 Comments

Thanks for the info. But I think json_encode() is more suitable in this case, as it is more compact.
@DennisMadsen -- Compactness aside, it is certainly more portable.
0

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

1 Comment

You'd want to pass the second argument to print_r(), forcing return over output; $string = print_r($array, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.