本文整理匯總了PHP中Set::__flatten方法的典型用法代碼示例。如果您正苦於以下問題:PHP Set::__flatten方法的具體用法?PHP Set::__flatten怎麽用?PHP Set::__flatten使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Set
的用法示例。
在下文中一共展示了Set::__flatten方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: sort
/**
* Sorts an array by any value, determined by a Set-compatible path
*
* @param array $data
* @param string $path A Set-compatible path to the array value
* @param string $dir asc/desc
* @return array
* @static
*/
function sort($data, $path, $dir)
{
$result = Set::__flatten(Set::extract($data, $path));
list($keys, $values) = array(Set::extract($result, '{n}.id'), Set::extract($result, '{n}.value'));
$dir = strtolower($dir);
if ($dir === 'asc') {
$dir = SORT_ASC;
} elseif ($dir === 'desc') {
$dir = SORT_DESC;
}
array_multisort($values, $dir, $keys, $dir);
$sorted = array();
$keys = array_unique($keys);
foreach ($keys as $k) {
$sorted[] = $data[$k];
}
return $sorted;
}
示例2: sort
/**
* Sorts an array by any value, determined by a Set-compatible path
*
* @param array $data An array of data to sort
* @param string $path A Set-compatible path to the array value
* @param string $dir Direction of sorting - either ascending (ASC), or descending (DESC)
* @return array Sorted array of data
*/
public static function sort($data, $path, $dir)
{
$originalKeys = array_keys($data);
if (is_numeric(implode('', $originalKeys))) {
$data = array_values($data);
}
$result = Set::__flatten(Set::extract($data, $path));
list($keys, $values) = array(Set::extract($result, '{n}.id'), Set::extract($result, '{n}.value'));
$dir = strtolower($dir);
if ($dir === 'asc') {
$dir = SORT_ASC;
} elseif ($dir === 'desc') {
$dir = SORT_DESC;
}
array_multisort($values, $dir, $keys, $dir);
$sorted = array();
$keys = array_unique($keys);
foreach ($keys as $k) {
$sorted[] = $data[$k];
}
return $sorted;
}