早上好,我只是摇摇欲坠但是,php有超过10种方法对数组进行排序,但我找不到一个按动态排序所需的键。
我不能设置像某些uksorts,因为它将动态填充。我在前面使用smarty(没有数组排序函数),我正在尝试使用一个可调用的静态来在一个特定的点上求助和打印排序的数组。
我已经对它应用了一些逻辑,我认为它会是这样的。
Smarty的:
{assign var='key' value='category_id'}
{assign var='direction' value='normal'}
{assign var='sortedItem' value=''}
{foreach $object.prop item="item"}
{sortedItem = Class::arraySortBy($item, $key, $direction)}
<a href="{$item['link']}">
{$item['name']}
</a>
{foreach}
PHP:
public static function arraySortBy($elemGroup, $sortBy, $direction) {
if(is_object($elemGroup)){
$elemGroup = (array) $elemGroup;
}
if ($direction == 'normal') {
// here is where i want to sort the elemGroup array by the key i want, as the key is product_id and i want to sort by category_id
} else if ($direction == 'reverse'){
// here the same but in reverse order
} else {
error_log('Direction not properly set.');
}
return $elemGroup;
}
事实是我想重新排序对象bu category_id,其次是product_id IE:
itemA{product_id=1, category_id=1}
itemB{product_id=2, category_id=2}
itemC{product_id=3, category_id=1}
itemE{product_id=4, category_id=1}
itemD{product_id=5, category_id=2}
结果:
itemA
itemB
itemC
itemE
itemD
预期结果
itemA
itemC
itemE
itemB
itemD
有没有办法用PHP排序功能来实现这一点,还是必须自定义?
谢谢
为什么你不能使用uasort
?
function custom_array_sort($arr, $sorts)
{
// Either pass an array of sorts, or every argument after the first one.
$sorts = is_array($sorts) ? $sorts : array_slice(func_get_args(), 1);
uasort($arr, function ($a, $b) use (&$arr, $sorts) {
for ($i = 0; $i < count($sorts); $i++) {
if ($a[$sorts[$i]] == $b[$sorts[$i]]) {
if (isset($sorts[$i + 1])) {
$arr = custom_array_sort($arr, array_slice($sorts, 1));
} else {
return 0;
}
} else {
return $a[$sorts[$i]] - $b[$sorts[$i]];
}
}
});
return $arr;
}
这首先比较category_id
字段。
如果这些是相同的,那么我们比较product_id
。使用减法使得较小的product_id
s在较大的之前排序。
如果category_id
s不同,那么我们在category_id
s上进行与product_id
相同的操作。
要在您的视图中实现此功能,请按照此documentation进行操作
$smarty->register_function('custom_array_sort', 'custom_array_sort_wrapper');
function custom_array_sort_wrapper($params, &$smarty)
{
if (empty($params['arr'])) {
$arr = [];
} else {
$arr = $params['arr'];
}
if (empty($params['sorts'])) {
$sorts = [];
} else {
$sorts = $params['sorts'];
}
return custom_array_sort($arr, $sorts);
}
然后可以通过以下方式在视图中使用它:
{custom_array_sort arr=$object sorts=['category_id', 'product_id']}
这个新实现的优点是您可以指定任意数量的列进行排序。 它还意味着您可以指定不同的列来排序不同的数组。
如果要按更多列排序,则只需将另一列名添加到$sorts
数组中。
你可以尝试这个功能。
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
例:
数组值: -
$item_array = array (
'itemA' => array('product_id'=>1, 'category_id'=>1),
'itemB' => array('product_id'=>2, 'category_id'=>2),
'itemC' => array('product_id'=>3, 'category_id'=>1),
'itemD' => array('product_id'=>4, 'category_id'=>1),
'itemE' => array('product_id'=>5, 'category_id'=>2)
);
使用功能: -
$itme_list = array_sort($item_array, 'category_id', SORT_ASC);
print_r($itme_list);
输出:
Array
(
[itemA] => Array
(
[product_id] => 1
[category_id] => 1
)
[itemC] => Array
(
[product_id] => 3
[category_id] => 1
)
[itemD] => Array
(
[product_id] => 4
[category_id] => 1
)
[itemB] => Array
(
[product_id] => 2
[category_id] => 2
)
[itemE] => Array
(
[product_id] => 5
[category_id] => 2
)
)