0

I have a need to print a complex PHP variable that can be pasted into code directly. print_r doesn't do it as when I use it, I get something like this:

Array
(
    [cost] => 218.16
    [discount] => Array(...)
    [description] => Cost Not Included
    [quantity] => 1
)

which cannot be immediately pasted into PHP code. I instead need something like so (with commas added, brackets changes to ampersands, etc)

array
(
    'cost' => '218.16',
    'discount' => array(...),
    'description' => 'Cost Not Included',
    'quantity' => 1,
),

How?

Currently I do it manually in my text editing program...

Dennis
  • 7,907
  • 11
  • 65
  • 115
  • possible duplicate of [Create array printed with print\_r](http://stackoverflow.com/questions/7025909/create-array-printed-with-print-r) – Rizier123 Jan 20 '15 at 15:09
  • linked question convert output of `print_r` into PHP readable form. Similar in a roundabout way, but not the same as printing out an array in PHP readable form as the first step. – Dennis Jan 20 '15 at 15:15

2 Answers2

4

Try :

var_export( $array );

It will print it.
Or if you want it in a string, then:

var_export($array, true);
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24
1

You can achieve that by using the var_export function as described from this post.

Community
  • 1
  • 1
ultrajohn
  • 2,527
  • 4
  • 31
  • 56