Created
June 5, 2014 08:56
-
-
Save krisanalfa/5d1fa07afd9456762daf to your computer and use it in GitHub Desktop.
Your Array Helper
This file contains 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 | |
class ArrayHelper | |
{ | |
public static function except($attributes, $hidden) | |
{ | |
return array_diff_key($attributes, array_flip((array) $hidden)); | |
} | |
public static function isEmpty($array) | |
{ | |
if(self::depth($array) > 0) | |
{ | |
$empty = true; | |
foreach ($array as $key => $value) $empty = (empty($value) or is_null($value)); | |
return $empty; | |
} | |
return empty($array); | |
} | |
public static function only($attributes, $hidden) | |
{ | |
return array_intersect_key($attributes, array_flip((array) $hidden)); | |
} | |
public static function replaceKey($attributes, $regex, $replacement = '') | |
{ | |
$array = array(); | |
foreach ($attributes as $key => $value) $array[str_replace($regex, $replacement, $key)] = $value; | |
return $array; | |
} | |
public static function massReplaceValue($attributes, $search, $replacement) | |
{ | |
$array = array(); | |
foreach ($attributes as $value) $array[] = str_replace($search, $replacement, $value); | |
return $array; | |
} | |
public static function depth($array) | |
{ | |
$maxIndentation = 1; | |
$arrayStr = print_r($array, true); | |
$lines = explode("\n", $arrayStr); | |
foreach ($lines as $line) { | |
$indentation = (strlen($line) - strlen(ltrim($line))) / 4; | |
if ($indentation > $maxIndentation) $maxIndentation = $indentation; | |
} | |
return ceil(($maxIndentation - 1) / 2) + 1; | |
} | |
public static function flip($array) | |
{ | |
$data = array(); | |
for ($i = 0; $i < count(reset($array)); $i++) { | |
$data[$i] = array(); | |
foreach ($array as $key => $value) $data[$i][$key] = $value[$i]; | |
} | |
return $data; | |
} | |
public static function onlyFalidFiles($files) { | |
$array = array(); | |
foreach ($files as $key => $value) { | |
if (! $value['error']) $array[$key] = $value; | |
} | |
return $array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment