Store var_dump in a variable

Asked

Viewed 1,475 times

2

I need to show the array and store it in a variable, the functions I know that do that are var_dump and print_r, but they do not store in string, they already dao echo.

What function to use?

2 answers

5

You can use $string = var_export($array, true); to save a string from the array

3


The correct function is the print_r() because it accepts a second parameter to pass the output instead of doing the dump of the same:

Example:

$b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));
$results = print_r($b, true);

Output to the variable $results:

Array
(
    [m] => monkey
    [foo] => bar
    [x] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )

)

The variable $results will become a string with the above content.

See example in Ideone.

  • I took your job and gave everything ok $results = is_array($execute) ? print_r($execute, true) : $execute;
 however when sending $Results by websockets it disconnects D:

  • 1

    This is another completely different and unaddressed problem in your initial question. Open a question about the problem of sending the output from print_r($arr, true) for websockets so that we can help you.

  • I decided to use json to send, more practical. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.