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

2
  • possible duplicate of Create array printed with print_r Commented Jan 20, 2015 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. Commented Jan 20, 2015 at 15:15

2 Answers 2

4

Try :

var_export( $array );

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

var_export($array, true);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.