Skip to content

Instantly share code, notes, and snippets.

@ineersa
Created February 22, 2016 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ineersa/07dfa03ac0fb6fa0d39d to your computer and use it in GitHub Desktop.
Save ineersa/07dfa03ac0fb6fa0d39d to your computer and use it in GitHub Desktop.
<?php
namespace app\components;
class Helpers
{
public static function getDays($month,$year)
{
if ($month==date("m")){
$num=(int)date("d");
}
else $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$output = array();
$day = $num+1;
$i=$num;
while ($i>=1)
{
$output[] = date('Y-m-d',mktime(0,0,0,$month,($day-$i),$year));
$i--;
}
return $output;
}
public static function isset_key($array, $key, $default = null)
{
return isset($array[$key]) ? $array[$key] : $default;
}
public static function getFileSizeInKb($file)
{
return number_format(filesize($file) / 1024, 2);
}
/**
* Recursively implodes an array with optional key inclusion
*
* Example of $include_keys output: key, value, key, value, key, value
*
* @access public
* @param array $array multi-dimensional array to recursively implode
* @param string $glue value that glues elements together
* @param string $outerGlue value that glues pares together
* @param bool $include_keys include keys before their values
* @param bool $trim_all trim ALL whitespace from string
* @return string imploded array
*/
public static function recursive_implode(array $array, $glue = '=', $outerGlue = "\n", $include_keys = true, $trim_all = false)
{
$glued_string = '';
array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string, $outerGlue)
{
$include_keys and $glued_string .= $key.$glue;
$glued_string .= $value.$outerGlue;
});
strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));
$trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);
return (string) $glued_string;
}
public static function flattenArray(array $array)
{
$ret_array = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value){
$ret_array[] = $value;
}
return $ret_array;
}
public static function traverseArray(&$array, $keys) {
foreach ($array as $key => &$value) {
if (is_array($value)) {
self::traverseArray($value, $keys);
} else {
if (in_array($key, $keys)){
unset($array[$key]);
}
}
}
}
public static function formatBytes($bytes, $precision = 2) {
return number_format($bytes / 1024, $precision);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment