Skip to content

Instantly share code, notes, and snippets.

@bastman
Created December 2, 2011 18:00
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 bastman/1424196 to your computer and use it in GitHub Desktop.
Save bastman/1424196 to your computer and use it in GitHub Desktop.
Lib_Utils_Array_List_Values
/**
* Created by JetBrains PhpStorm.
* User: seb
* Date: 02.11.11
* Time: 09:55
* To change this template use File | Settings | File Templates.
*/
class Lib_Utils_Array_List_Values
{
/**
* unique=true: [1,2,3,4] + [3,4,5,6,7] = [1,2,3,4,5,6,7]
* @static
* @throws Exception
* @param $array1
* @param $array2
* @param $unique
* @return array
*/
public static function append($array, $valuesList, $unique)
{
if (!is_bool($unique)) {
throw new Exception("Invalid parameter 'unique' at ".__METHOD__);
}
$array1 = $array;
$array2 = $valuesList;
if (!is_array($array1)) {
$array1 = array();
}
if (!is_array($array2)) {
$array1 = array();
}
$result = (array)$array1;
foreach($array2 as $key => $value) {
if ($unique===true) {
if (in_array($value, $array1, true)) {
//drop value
continue;
}
}
// use value
$result[] = $value;
}
return $result;
}
/**
* [1,2,3,4] - [3,4,5,6,7] = [1,2]
* @static
* @param $array1
* @param $array2
* @return array
*/
public static function remove($array, $valuesList, $unique)
{
if (!is_bool($unique)) {
throw new Exception("Invalid parameter 'unique' at ".__METHOD__);
}
$result = array();
$array1 = $array;
$array2 = $valuesList;
if (!is_array($array1)) {
$array1 = array();
}
if (!is_array($array2)) {
$array1 = array();
}
// [1,2,3,4] - [3,4,5,6,7] = [1,2]
foreach($array1 as $key => $value) {
if (in_array($value, $array2, true)) {
//drop value
continue;
}
if ($unique===true) {
if (in_array($value, $result, true)) {
//drop value
continue;
}
}
// use value
$result[] = $value;
}
return $result;
}
/**
*
* find all values from array2 that are not in array1
* [1,2,3] -vs- [3,4,5] = [4,5]
* @static
* @throws Exception
* @param $array1
* @param $array2
* @param $unique
* @return array
*/
public static function whatsMissing($array, $searchForValuesList, $unique)
{
if (!is_bool($unique)) {
throw new Exception("Invalid parameter 'unique' at ".__METHOD__);
}
$result = array();
if (!is_array($array)) {
$array = array();
}
if (!is_array($searchForValuesList)) {
$searchForValuesList = array();
}
foreach($searchForValuesList as $value) {
if(!in_array($value, $array, true)) {
if ($unique===true) {
if (!in_array($value, $result, true)) {
$result[] = $value;
}
} else {
$result[] = $value;
}
}
}
if ($unique===true) {
$result=(array)array_unique($result);
}
return $result;
}
/**
* e.g. returns the first 100 ($count) values of a given list
* @static
* @throws Exception
* @param $array
* @param int $count
* @param bool $unique
* @return array
*/
public static function collectSome($array, $count, $unique)
{
if (!is_bool($unique)) {
throw new Exception("Invalid parameter 'unique' at ".__METHOD__);
}
$result = array();
if (!is_array($array)) {
$array = array();
}
if (!is_int($count)) {
return $result;
}
if ($count<1) {
return $result;
}
$cnt = 0;
foreach($array as $key => $value) {
if ($cnt >= $count) {
break;
}
if ($unique===true) {
if (in_array($value, $result, true)) {
// drop value
continue;
}
}
$result[] = $value;
$cnt ++;
}
return $result;
}
/**
* @static
* @param array|null $array
* @param int $count
* @param mixed $padValue
* @return array
*/
public static function pad($array, $count, $padValue)
{
if (!is_array($array)) {
$array = array();
}
$result = $array;
if (!is_int($count)) {
return $result;
}
$result = array_pad($array, $count, $padValue);
return $result;
}
/**
* @static
* @param array|niúll|mixed $array
* @return array
*/
public static function ensureList($array)
{
$result = array();
if (!is_array($array)) {
return $result;
}
foreach($array as $value) {
$result[] = $value;
}
return $result;
}
/**
* @static
* @param int $minValue
* @param int $maxValue
* @param int|null $stepSize
* @return array
*/
public static function range($minValue, $maxValue, $stepSize)
{
$result = (array)range($minValue, $maxValue, $stepSize);
return $result;
}
/**
* @static
* @param array $array
* @param int $maxTimes
* @return array
*/
public static function shuffle($array, $maxTimes)
{
$result = array();
if (!is_array($array)) {
return $result;
}
if (count($array)<1) {
return $result;
}
$maxTimes = (int)$maxTimes;
if ($maxTimes<0) {
return $array;
}
if ($maxTimes>255) {
$maxTimes = 255;
}
$list = array();
foreach($array as $item) {
$list[] = $item;
}
$shuffleTimes = rand(1, $maxTimes);
for ($i=0; $i<$shuffleTimes; $i++) {
shuffle($list);
}
$result = $list;
return (array)$result;
}
/**
* @static
* @throws Exception
* @param $array
* @param $valuesList
* @return array
*/
public static function keep($array, $valuesList, $unique)
{
if(!is_bool($unique)) {
throw new Exception("Invalid parameter 'unique' at ".__METHOD__);
}
$result = array();
if($valuesList === null) {
$valuesList = array();
}
if (!is_array($valuesList)) {
throw new Exception("Invalid parameter valuesList at ".__METHOD__);
}
foreach($array as $value) {
if (in_array($value, $valuesList, true)) {
if ($unique === true) {
if(!in_array($value, $result, true)) {
$result[] = $value;
}
} else {
$result[] = $value;
}
}
}
return $result;
}
/**
* @static
* @throws Exception
* @param array|null $array
* @return string
*/
public static function checksum($array)
{
$json = json_encode($array);
if (!is_string($json)) {
throw new Exception("Error while serialize to json at ".__METHOD__);
}
$checksum = md5($json);
return $checksum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment