PHP对象到数组按关联键排序

PHP对象到数组按关联键排序,php,smarty,Php,Smarty,早上好,我对此感到震惊,但是php有10多种方法可以对数组进行排序,但我找不到一种方法可以根据所需的键进行动态排序 我不能设置一些类似uksorts的内容,因为它将动态填充。我在前面使用smarty(它没有数组排序函数),我试图创建一个可调用的静态函数,在一个特定点上使用并打印排序的数组 我在上面应用了一些逻辑,我想应该是这样的 Smarty: {assign var='key' value='category_id'} {assign var='direction' value='no

早上好,我对此感到震惊,但是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}
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;
}
itemA
itemB
itemC
itemE
itemD
事实上,我想重新排序对象bu category_id,其次是product_id,即:

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} 
结果:

  {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}
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;
}
itemA
itemB
itemC
itemE
itemD
预期结果

itemA
itemC
itemE
itemB
itemD
有没有一种方法可以通过PHP排序函数实现这一点,或者它必须是定制的


谢谢

您可以尝试此功能

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
        )

)
注:参考链接:-

为什么你不能使用

函数自定义数组排序($arr,$sorts)
{
//要么传递排序数组,要么传递第一个数组后的每个参数。
$sorts=is_数组($sorts)?$sorts:array_切片(func_get_args(),1);
uasort($arr,函数($a,$b)使用(&$arr,$sorts){
对于($i=0;$i

这是通过首先比较
类别id
字段来实现的。
如果这些相同,则比较
产品id
。使用减法是为了将
产品id
s中较小的一个排序在较大的一个之前

如果
category\u id
s不相同,则我们对
category\u id
s执行与上文对
product\u id
相同的操作


要在视图中实现这一点,请遵循以下步骤

$smarty->register_函数('custom_array_sort','custom_array_sort_wrapper');
函数自定义数组排序包装($params,&$smarty)
{
if(空($params['arr'])){
$arr=[];
}否则{
$arr=$params['arr'];
}
if(空($params['sorts'])){
$sorts=[];
}否则{
$sorts=$params['sorts'];
}
返回自定义数组排序($arr,$sorts);
}
然后,可以通过以下方式在视图中使用此选项:

{custom_array_sort arr=$object sorts=['category_id', 'product_id']}
这个新实现的优点是,您可以指定任意数量的列进行排序。
这还意味着您可以为不同的数组指定不同的列进行排序


如果您想按更多列进行排序,那么只需向
$sorts
数组添加另一个列名。

什么代码会产生结果?“我无法设置类似于某些uksorts的内容,因为它将动态填充。”-这是什么意思?@splash58没有代码可以复制结果。结果是默认的…@04FS我的意思是,如果您必须对每个要排序的键进行自定义排序,这将非常繁琐,我希望它可以是一些php内置函数,如ksort(array、assocKey、direction)这样做相当容易。仅代码和仅链接的答案在IMO中不是答案。一个好的答案应该解释问题和解决方案,以便OP和未来的访问者能够learn@treyBake:谢谢你的建议。我会处理好的这正是我不想要的。我的意思是,如果PHP上有一个内置函数可以实现这一点,而无需进行自定义比较,因为这很容易实现,但当您需要按不同的需要重复多次时,这会让我在每次需要按不同的键排序时创建一个新的soert函数。我知道并尝试过不同的php文档数组排序函数,我正在寻找更有用的东西。我想我必须为它制作一个lib,但如果有内置的东西,它可以提供比我在php文档上看到的简单的“按第一个键排序或DIY”更多的功能,那就好了…@JoelBonetRodríguez我不理解你的请求了。。。如果您想要自定义排序,那么我可以相应地修改函数,以便您可以更改排序的列。。。您的问题并没有明确说明您可能希望在不同的时间按不同的列进行排序。@JoelBonetRodríguez我已经更新了代码,请检查是否更适合您的应用程序。