Last active
October 12, 2018 20:24
-
-
Save vijinho/77d36557ab6de1ec105ea3f1b186b6d4 to your computer and use it in GitHub Desktop.
PHP simple load, save functions for file to php array with value and charset type conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Clear an array of empty values | |
* | |
* @param array $keys array keys to explicitly remove regardless | |
* @return array the trimmed down array | |
*/ | |
function array_clear($array, $keys = []) | |
{ | |
foreach ($array as $key => $value) { | |
if (is_array($value)) { | |
do { | |
$oldvalue = $value; | |
$value = array_clear($value, $keys); | |
} | |
while ($oldvalue !== $value); | |
$array[$key] = array_clear($value, $keys); | |
} | |
if (empty($value) && 0 !== $value) { | |
unset($array[$key]); | |
} | |
if (in_array($key, $keys, true)) { | |
unset($array[$key]); | |
} | |
} | |
return $array; | |
} | |
/** | |
* Encode array character encoding recursively | |
* | |
* @param mixed $data | |
* @param string $to_charset convert to encoding | |
* @param string $from_charset convert from encoding | |
* @return mixed | |
*/ | |
function to_charset($data, $to_charset = 'UTF-8', $from_charset = 'auto') | |
{ | |
if (is_numeric($data)) { | |
if (is_float($data)) { | |
return (float) $data; | |
} else { | |
return (int) $data; | |
} | |
} else if (is_string($data)) { | |
return mb_convert_encoding($data, $to_charset, $from_charset); | |
} else if (is_array($data)) { | |
foreach ($data as $key => $value) { | |
$data[$key] = to_charset($value, $to_charset, $from_charset); | |
} | |
} else if (is_object($data)) { | |
foreach ($data as $key => $value) { | |
$data->$key = to_charset($value, $to_charset, $from_charset); | |
} | |
} | |
return $data; | |
} | |
/** | |
* Load a json file and return a php array of the content | |
* | |
* @param string $file the json filename | |
* @return string|array error string or data array | |
*/ | |
function json_load($file) | |
{ | |
$data = []; | |
if (file_exists($file)) { | |
$data = to_charset(file_get_contents($file)); | |
$data = json_decode( | |
mb_convert_encoding($data, 'UTF-8', "auto"), true, 512, | |
JSON_OBJECT_AS_ARRAY || JSON_BIGINT_AS_STRING | |
); | |
} | |
if (null === $data) { | |
return json_last_error_msg(); | |
} | |
if (is_array($data)) { | |
$data = to_charset($data); | |
} | |
return $data; | |
} | |
/** | |
* Save data array to a json | |
* | |
* @param string $file the json filename | |
* @param array $data data to save | |
* @param string optional $prepend string to prepend in the file | |
* @param string optional $append string to append to the file | |
* @return boolean true|string TRUE if success or string error message | |
*/ | |
function json_save($file, $data, $prepend = '', $append = '') | |
{ | |
if (empty($data)) { | |
return 'No data to write to file.'; | |
} | |
if (is_array($data)) { | |
$data = to_charset($data); | |
} | |
if (!file_put_contents($file, | |
$prepend . json_encode($data, JSON_PRETTY_PRINT) . $append)) { | |
$error = json_last_error_msg(); | |
if (empty($error)) { | |
$error = sprintf("Unknown Error writing file: '%s' (Prepend: '%s', Append: '%s')", | |
$file, $prepend, $append); | |
} | |
return $error; | |
} | |
return true; | |
} | |
/** | |
* Load a serialized php data file and return it | |
* | |
* @param string $filename the json filename | |
* @return array $data | |
*/ | |
function serialize_load($file) | |
{ | |
if (file_exists($file)) { | |
$data = unserialize(file_get_contents($file)); | |
} | |
return empty($data) ? [] : $data; | |
} | |
/** | |
* Save data array to a php serialized data | |
* | |
* @param string $filename the json filename | |
* @param array $data data to save | |
* @return boolean result | |
*/ | |
function serialize_save($file, $data) | |
{ | |
if (empty($data)) { | |
return 'No data to write to file.'; | |
} | |
return file_put_contents($file, serialize($data)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment