Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print an array as code

Tags:

php

yaml

I want to convert a big yaml file to PHP array source code. I can read in the yaml code and get back a PHP array, but with var_dump($array) I get pseudo code as output. I would like to print the array as valid php code, so I can copy paste it in my project and ditch the yaml.

like image 723
Ward Bekker Avatar asked Feb 28 '11 08:02

Ward Bekker


People also ask

Can you print an array in C++?

We can use Ostream iterators to write to an output stream. The idea is to use an output iterator std::ostream_iterator to print array contents to std::cout with the help of std::copy , which takes input iterator to the starting and ending positions of the array and the output iterator.

How do you print an array in Python?

To print an array in Python, use the print() function. The print() is a built-in Python function that takes the name of the array containing the values and prints it. To create an array in Python, use the numpy library and create an array using the np. array() function, and then print that array in the console.


2 Answers

You're looking for var_export.

like image 171
deceze Avatar answered Sep 17 '22 09:09

deceze


You could use var_export, serialize (with unserialize on the reserving end), or even json_encode (and use json_decode on the receiving end). The last one has the advantage of producing output that can be processed by anything that can handle JSON.

like image 26
GordonM Avatar answered Sep 21 '22 09:09

GordonM