优化评级数组

问题描述:

I have a rating system on my Wordpress that came with the theme. The maximum rating possible is 10, so I wanted to edit this and make a 100 possible rating. So I edited this part:

public static function max_rating( $listing_id = null ) {
    $default = 100;

So now it understands that the max possible rating is 100. But under the rating array, there were these lines:

    $rating_options = array(
        '1' => 1,
        '2' => 2,
        '3' => 3,
        '4' => 4,
        '5' => 5,
        '6' => 6,
        '7' => 7,
        '8' => 8,
        '9' => 9,
        '10' => 10,

Which understand that the maximum possible rating is 10. Now I want to make a maximum rating of 100, but adding '11' => 11, '12' => 12, '13' => 13 etc takes a lot of time and it consumes a lot of space in my file. Is there a possiblity to shorten this or do I really have to enter every rating up to 100?

我的Wordpress上有一个主题附带的评级系统。 可能的最大评级为10,所以我想编辑它并进行100评级。 我编辑了这部分: p>

  public static function max_rating($ listing_id = null  ){
 $ default = 100; 
  code>  pre> 
 
 

所以现在它了解最大可能评级是100. 但是在评级数组下,有这些行: p>

  $ rating_options = array(
'1'=> 1,
'2'=> 2,
'3'=> 3,\  n'4'=> 4,
'5'=> 5,
'6'=> 6,
'7'=> 7,
'8'=> 8,\  n'9'=> 9,
'10'=> 10,
  code>  pre> 
 
 

了解最大可能评级为10.现在我想要 最高评级为100,但添加'11'=> 11,'12'=> 12,'13'=> 13 code>等需要花费大量时间并且耗费很多 在我的文件中有空间。是否有可能缩短这一点,或者我是否真的需要输入最高达100的每个等级? p> div>

you can use PHP's range function:

$ratings = range(0, 100);

reference: https://secure.php.net/manual/en/function.range.php

The accepted answer points you in the right direction.

Additional I would advice using array_combine like so:

$range = range(1,100);
$rating_options = array_combine($range, $range);
// array(1=>1, 2=>2, ...)

This way, your keys will be the same as the values.