嘿伙计们,我有一些关于嵌套函数的严重黑客代码,但由于某种原因,我无法让它与闭包或 lambda 函数一起工作,我认为这会更好,有人能帮我理解如何打开这个进入一个闭包,lambda或其他东西,以免用不需要的BS污染我的全局范围?
这是我的两种方法
/**
* Sort an array of object is ASC order.
* @param object $objArray An array of objects
* @return object Sorted array of objects
*/
protected function objSort($objArray)
{
// turns an Object Array into and Assoc Array
$array = self::objToArr($objArray);
// avoid recursively declaring the function
if (! function_exists('dsort'))
{
// nested functions are stupid.
function dsort(&$array)
{
foreach ($array AS &$current)
{
if (is_array($current)) dsort($current);
}
ksort($array);
}
}
dsort($array);
// back to an Object Array
return $objArray = json_decode(json_encode($array));
}
/**
* Turns an object array into an associative multidimensional array.
* @param object $object An array of objects
* @return array An associative array
*/
private static function objToArr($object)
{
$array = array();
$arrayObject = is_object($object) ? get_object_vars($object) : $object;
foreach ($arrayObject as $key => $value)
{
$value = (is_array($value) || is_object($value)) ? self::objToArr($value) : $value;
$array[$key] = $value;
}
return $array;
}
它们将获取一个对象数组,按字母顺序将它们排序为关联数组,然后将它们作为对象数组返回。