【问题标题】:How to sort an array based on another value?如何根据另一个值对数组进行排序?
【发布时间】:2021-03-26 18:28:57
【问题描述】:

所以我在这里很迷茫,因为我不知道我的对象是什么样子(不知道为什么我不能使用print_r() 来打印对象)。

基本上从下面的代码中,我需要根据$percent_avg$_reviews 的评论进行排序。我从多个 for 循环中获得了 $percent_avg 值,这使事情变得复杂。我不确定如何根据 $percent_avg 变量创建一个新的排序数组。

$_reviews = $block->getReviewCollection($_item->getID());
$sorted_reviews = [];
$sorted_percentage = [];
foreach ($_reviews as $_review) {
    $total_percent = 0;
    foreach ($_review->getRatingVotes() as $_vote){
        $total_percent += $_vote->getPercent();
    }
    $percent_avg = $total_percent/3;
    array_push($sorted_percentage, $percent_avg);
}

我已经尝试使用上面的代码对评论进行排序,我认为我可以为排序的$percent_avg 创建一个新数组并将该值与$_reviews 对象进行比较?但是这样的话,会很慢吗?

//sort array
sort($sorted_percentage);

//run a for loop - if the value in $sorted_percentage matches $percent_avg, push it to a new array
foreach ($sorted_percentage as $percent) {
                foreach ($_reviews as $_review) {
                    $total_percent = 0;
                    foreach ($_review->getRatingVotes() as $_vote){
                        $total_percent += $_vote->getPercent();
                    }
                    $percent_avg = $total_percent/3;
                    if($percent_avg == $percent){
                        array_push($sorted_reviews, $_review);
                    }
                }
            }

以上只是一个想法,它不起作用。

我还是 PHP 新手,所以任何建议/帮助将不胜感激。谢谢。

【问题讨论】:

标签: php arrays sorting object


【解决方案1】:

如果要根据每条评论的getRatingsVotes()'getPercent()之和对$_reviews进行排序,可以使用usort()

usort( $_reviews, function( $a, $b ) {
    $a_total = 0;
    foreach ( $a->getRatingVotes() as $_vote ) {
        $a_total += $_vote->getPercent();
    }

    $b_total = 0;
    foreach ( $b->getRatingVotes() as $_vote ) {
        $b_total += $_vote->getPercent();
    }

    return $a_total <=> $b_total;
});

如果$_reviews 包含许多元素,或者如果个别评论往往包含许多投票,这可能会很慢。您可以通过缓存 $a_total$b_total 并尽可能使用这些缓存的数字来加快速度:

usort( $_reviews, function( $a, $b ) {
    if ( array_key_exists( 'total', $a ) ) {
        // Fetch from cache:
        $a_total = $a['total'];
    } else {
        // Calculate and store in cache:
        $a_total = 0;
        foreach ( $a->getRatingVotes() as $_vote ) {
            $a_total += $_vote->getPercent();
        }
        $a['total'] = $a_total;
    }

    if ( array_key_exists( 'b_total', $b ) ) {
        // Fetch from cache:
        $b_total = $b['total'];
    } else {
        // Calculate and store in cache:
        $b_total = 0;
        foreach ( $b->getRatingVotes() as $_vote ) {
            $b_total += $_vote->getPercent();
        }
        $b['total'] = $b_total;
    }

    return $a_total <=> $b_total;
});

【讨论】:

  • 非常感谢您!如果你不介意,你能解释一下第一个代码吗?具体来说,我不明白为什么会有function($a, $b),$a 和$b 是怎么来的?对不起愚蠢的问题,但我还在学习:)
  • PHP 的 usort() 接受一个回调函数,该函数将传递两个数组元素(这由 PHP 自动发生),其工作是返回一个数字,指示哪个具有更高的值。在我的代码中,回调是一个匿名函数(即未命名),并且我已将参数命名为$a$b。您可以将这些名称更改为您想要的任何名称。请参阅usort() 文档以获取有关回调函数及其预期工作方式的更多详细信息。
猜你喜欢
  • 2015-08-05
  • 2013-10-22
  • 2022-12-18
  • 2021-04-05
  • 1970-01-01
  • 2015-04-17
  • 2023-02-02
  • 2015-08-25
  • 1970-01-01
相关资源
最近更新 更多