Skip to content

Instantly share code, notes, and snippets.

@juniorb2ss
Last active December 28, 2015 02:49
Show Gist options
  • Save juniorb2ss/7431040 to your computer and use it in GitHub Desktop.
Save juniorb2ss/7431040 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* save json
* @param Array | $array | array
* @param String | $dir | dir location default: applcation/cache/
* @return json
*/
function save($array, $dir = NULL)
{
/**
* param passed?
* set default dir path
*/
if(empty($dir))
{
$dir = APPPATH;
$config_path = config_item('cache_path');
$dir = (!empty($config_path)) ? $dir.$config_path : $dir.'cache/score.cache';
}
/**
* file exist?
*/
if(file_exists($dir))
{
$json = file_get_contents($dir);
$json = (array)json_decode($json);
foreach ($json as $key => $value)
{
if($key == $array['name'])
{
unset($json[$key]);
$sjson[$key]['name'] = $array['name'];
$sjson[$key]['score'] = $array['score'];
$sjson[$key]['comment'] = $array['comment'];
break;
}
else
{
$sjson[$array['name']]['name'] = $array['name'];
$sjson[$array['name']]['score'] = $array['score'];
$sjson[$array['name']]['comment'] = $array['comment'];
}
}
$json = array_merge($json, $sjson);
$json = json_encode($json);
file_put_contents($dir, $json);
}
else
{
$json = json_encode(array($array['name'] => $array));
file_put_contents($dir, $json);
}
return $json;
}
/* End of file json.php */
/* Location: ./application/helpers/json.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment