So I am pretty lost here because I don't know what my object looks like (not sure why I can't do print_r()
to print the object).
Basically from the below code, I need to sort the reviews which is $_reviews
based on $percent_avg
. I got the $percent_avg
value from a multiple for loops and that's what complicates things. I am not sure how can I make a new sorted array based on that $percent_avg
variable.
$_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);
}
I have attempted the above code to sort the reviews, what I think is that I can make a new array for a sorted $percent_avg
and compare that value with the $_reviews
object? But with this, it will be pretty slow?
//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);
}
}
}
The above is just an idea and it is not working.
I am still new to PHP, so any suggestions/helps will be appreciated. Thank you.