PHP如何根据优先级排序对象数组并按字母顺序备份? [英] PHP How to sort an array of objects based on priority order with a backup of alphabetical?

查看:49
本文介绍了PHP如何根据优先级排序对象数组并按字母顺序备份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组:

I have an array like this:

$sort_me = array(
   array("file"=>"Desert.jpg"), 
   array("file"=>"Hello.jpg"), 
   array("file"=>"Test.jpg)
)

我想根据文件属性按字母顺序对该数组进行排序.为了使事情复杂化,我有一个可选的数组:

I want to sort this array based on the file attribute alphabetically. To complicate matters I have an optional array:

$sort_order = array("Test.jpg", "Hello.jpg", "NotFound.jpg")

此数组将指定一些用户定义的顺序.这些数组元素具有优先级,如果找到,将按该顺序放置.其他与 $ sort_order 中的内容不匹配的元素应按字母顺序在数组的末尾排序.

This array will specify some user defined ordering. These array elements have priority, and will be placed in that order if they are found. Other elements not matching anything in $sort_order should be ordered at the end of the array alphabetically.

在javascript中,我将使用带有2个元素的比较器函数的调用排序函数,并返回一个数字以将第一个对象放在第二个对象的前面或后面.

In javascript I would use call a sort function with a comparator function that takes 2 elements and returns a number to place the first object ahead or behind the second.

如何使用PHP做到这一点?

How can I do this with PHP?

修改

我尝试了一些操作,但没有成功.(再次修改以将逻辑合并到一个函数中,并概括出要按哪个字段排序)

I attempted something and it didn't work. (Modified again to put the logic into one function, and generalize which field to sort by)

<?php
    function sort_priority($sort_me, $sort_order, $field) {
        function compare($a, $b) {
            if ($sort_order) {
                $ai = array_search($a[$field], $sort_order);
                $bi = array_search($b[$field], $sort_order);
                if ($ai !== false && $bi === false) {
                    return -1;
                } else if ($bi !== false && $ai === false) {
                    return 1;
                } else if ($ai !== false && $bi !== false) {
                    return $bi - $ai;
                }
            }
            return $a[$field] < $b[$field] ? -1 : 1;
        }
        usort($sort_me, "compare");
    }

    $sort_order = array("Test.jpg", "Hello.jpg", "NotFound.jpg");
    $sort_me = array(
       array("file"=>"Test.jpg"),
       array("file"=>"Desert.jpg"),
       array("file"=>"Hello.jpg")
    );

    sort_priority($sort_me, $sort_order, "file");
    echo json_encode($sort_me);
?>

此输出

 Notice: Undefined variable: sort_order in c:\workspace\test.php on line 10

预期输出是

[{"file":"Test.jpg"},{"file":"Hello.jpg"},{"file":"Desert.jpg"}]

我不知道如何获取 compare 函数以正确使用上下文特定的 $ sort_order 函数.

I don't know how to get the compare function to properly use the context specific $sort_order function.

修改

我接受了一个答案,但是为了完整起见,我想发布一下我最终得出的结果似乎可行.如果有人想发布更优雅的解决方案,我会考虑将其标记为已接受.但是,这就是我所拥有的:

I accepted an answer, but for completeness I wanted to post what I finally ended up with that seemed to work. If anyone wants to post a more elegant solution, I would consider marking it as accepted. But here is what I have:

<?php
    function compare_priority($a, $b) {
        global $g_order, $g_field;
        if ($g_order) {
            $ai = array_search($a[$g_field], $g_order);
            $bi = array_search($b[$g_field], $g_order);
            if ($ai !== false && $bi === false) {
                return -1;
            } else if ($bi !== false && $ai === false) {
                return 1;
            } else if ($ai !== false && $bi !== false) {
                return $ai - $bi;
            }
        }
        return $a[$g_field] < $b[$g_field] ? -1 : 1;
    }

    function sort_priority(&$sort_me, $sort_order, $field) {
        global $g_order, $g_field;
        $g_order = $sort_order;
        $g_field = $field;
        usort($sort_me, "compare_priority");
    }

    $sort_me = array(
       array("file"=>"Z"), 
       array("file"=>"A"), 
       array("file"=>"Y"), 
       array("file"=>"B")
    );
    $sort_order = array("Z", "Y", "C");
    sort_priority($sort_me, $sort_order, "file");
    echo json_encode($sort_me);
?>

推荐答案

您可以执行与javascript相同的操作.usort( http://ch2.php.net/manual/en/function.usort.php )允许您定义元素之间的自定义比较.

You can do the same as you would have done with javascript. usort (http://ch2.php.net/manual/en/function.usort.php) allow you to define your custom comparison between elements.

我修改了您的代码,它似乎可以工作:

I modified your code and it seems to work :

<?php
    function test() {
        $sort_me = array(
           array("file"=>"Test.jpg"),
           array("file"=>"Desert.jpg"),
           array("file"=>"Hello.jpg")
        );
        global $sort_order;
        $sort_order = array("Test.jpg" , "Hello.jpg", "NotFound.jpg");
        function compare($a, $b) {
            global $sort_order;
            if (is_array($sort_order)) {
                $ai = array_search($a["file"], $sort_order);
                $bi = array_search($b["file"], $sort_order);
                if ($ai !== false && $bi === false) {
                    return -1;
                } else if ($bi !== false && $ai === false) {
                    return 1;
                } else if ($ai !== false && $bi !== false) {
                    return $ai - $bi;
                }
            }
            return $a["file"] < $b["file"] ? -1 : 1;
        }
        usort($sort_me, "compare");
        echo json_encode($sort_me);
    }

    test();
?>

这篇关于PHP如何根据优先级排序对象数组并按字母顺序备份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆