The purpose of this package is to print an array to a file in a reader-friendly format, that can later be included as php. The package supports deeply nested arrays, with numeric, string, boolean and object values.
You can install the package via composer:
composer require kfriars/php-array-to-file
You can use the static method toFile(...)
on Kfriars\ArrayToFile\ArrayWriter
for convenient use, or you can inject the Kfriars\ArrayToFile\ArrayToFile
class as a dependency, and use write(...)
.
An example of use:
ArrayWriter::toFile([1, 2, 3], '/absolute/path/to/file.php');
Would create /absolute/path/to/file.php
with the contents:
<?php
return [
1,
2,
3,
];
The package also allows you to transform the values in your array by passing in a callable. The callable receives the value before it is written to the file, and should return the value you desire to have written. You can use it like:
function save(ArrayToFile $a2f)
{
$a2f->write([0, 1, '', ' '], '/absolute/path/to/file.php', function ($value) {
return (bool) $value;
});
}
Which will create /absolute/path/to/file.php
with the contents:
<?php
return [
false,
true,
false,
true,
];
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email nyxsoft.inc@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.