M
M
Maxim Timofeev2015-08-02 14:41:35
Yii
Maxim Timofeev, 2015-08-02 14:41:35

How to write php file with array?

I have a php file with the following content:

<?php
return [
    'ID' => 'id',
    'User ID' => 'user id',
    'Total Price' => 'Total Price',
];

This is the translation file in yii2 if interested.
I create an array from it, change the values, and then I can't save it. What function in php can save a php file like this? Or should it be broken down into lines?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dave, 2015-08-02
@webinar

There is a built-in function var_export()
for this. To solve your problem, write something similar to this:

$filename = __DIR__.'/data.php';  // Путь куда записать содержимое файла

$string = "<?php\n return ".var_export($data, true).';';
file_put_contents($filename, $string);

Where $data is the current or current and modified read array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question