Store var_dump in a variable

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

What function to use?

    
asked by anonymous 25.12.2014 / 17:02

2 answers

3

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

Example:

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

Output to 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 displayed.

See example on Ideone .

    
25.12.2014 / 17:14
4

You can use $string = var_export($array, true); to save an array string

    
25.12.2014 / 19:56