【问题标题】:two php arrays - sort one array with the value order of another两个 php 数组 - 用另一个数组的值顺序对一个数组进行排序
【发布时间】:2011-06-06 05:57:37
【问题描述】:

我有两个这样的 PHP 数组:

  1. 包含 ID 的 X 记录数组 的 Wordpress 帖子(在特定的 顺序)
  2. Wordpress 帖子数组

这两个数组看起来像这样:

数组一(Wordpress 帖子 ID 的排序自定义数组)

Array (  
  [0] => 54
  [1] => 10
  [2] => 4
)

数组二(Wordpress 帖子数组)

Array ( 
    [0] => stdClass Object
        (
            [ID] => 4
            [post_author] => 1
    )
    [1] => stdClass Object
        (
            [ID] => 54
            [post_author] => 1
    )
    [2] => stdClass Object
        (
            [ID] => 10
            [post_author] => 1
    )
)

我想按照第一个数组中 ID 的顺序对 wordpress 帖子数组进行排序。

我希望这是有道理的,并在此先感谢您的帮助。

汤姆

编辑:服务器运行 PHP 版本 5.2.14

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    您可以创建一个嵌套循环机制来匹配订单和 id 并重建一个新的帖子数组。

    $new_post_array = array();
    
    foreach($id_array as $id) {          //loop through custom ordered ids
    
        foreach($post_array as $post) {  //for every id loop through posts
    
            if($id == $post->ID){         //and when the custom ordered id matches the post->ID
    
                new_array[] = $post       //push the post on the new array
    
            }
    
        }
    
    }
    

    【讨论】:

    • 复制两个数组(这就是 foreach 所做的)在这里是不必要的。
    • 感谢您的回答。这可以完成这项工作,但似乎有点迟钝?无论如何我已经接受了答案,因为它确实可以满足我的要求,并且可以与 PHP 5.2 一起使用
    • 抱歉@jondavidjohn,我不得不切换到更快的解决方案。再次感谢。
    【解决方案2】:

    使用usort 应该很容易,它使用用户定义的比较函数对数组进行排序。结果可能如下所示:

    usort($posts, function($a, $b) use ($post_ids) {
        return array_search($a->ID, $post_ids) - array_search($b->ID, $post_ids);
    });
    

    请注意,由于此解决方案使用 anonymous functions and closures,因此需要 PHP 5.3。


    对于 5.3 之前的版本(黑暗时代!),一个简单的解决方案是使用快速循环然后ksort

    $ret = array();
    $post_ids = array_flip($post_ids);
    foreach ($posts as $post) {
        $ret[$post_ids[$post->ID]] = $post;
    }
    ksort($ret);
    

    【讨论】:

    • 我输入了完全相同的解决方案,+1。 PS:我认为你需要在这个函数之前和之后array_flip $post_ids。
    • @Richard Tuin 这将是一种解决方案——另一种是使用array_search,因为我正在纠正我的帖子以供使用。
    • 这看起来是一个非常优雅的解决方案,但是,当我尝试实现它时,我对函数应该做什么感到困惑?如果我直接从这里复制它,我会得到: Parse error: syntax error, unexpected T_FUNCTION ---- 我觉得我应该创建一个函数来进行比较? (请原谅误解......我无疑有一个迟钝的时刻)......编辑:阅读这个php.net/manual/en/function.usort.php似乎让它更清楚一点,但仍然卡住!
    • @Tisch $posts 是您的帖子对象数组; $post_ids 是您的 ID 值数组。要了解usort,阅读我在答案中链接到的手册页可能是最简单的。如果您仍然不确定,也许您可​​以澄清您不确定的内容?此解决方案要求您拥有 PHP 5.3 - 我将编辑我的答案以反映这一点。
    • 对于 5.3 之前的解决方案,您可以使用 array_flip() 而不是 array_search,这对于对大列表进行排序会更好
    【解决方案3】:
    $sortOrderMap = array_flip($postIds);
    
    usort($posts, function($postA, $postB) use ($sortOrderMap) {
        return $sortOrderMap[$postA->ID] - $sortOrderMap[$postB->ID];
    });
    

    你可以简单地从 a 中减去 b 而不是从 b 中减去 a 来对另一个方向进行排序

    【讨论】:

    • 也感谢您的回答...上面的解决方案非常相似,因此如果可能,我将继续使用该解决方案。看起来 PHP 5.2 阻止了我:(
    猜你喜欢
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多