组合 2 PHP 个数组来创建排行榜

Combine 2 PHP arrays to create a leaderboard

我有 2 个数组 - 经销商和分数:

经销商

Array(
    [0] => Test Dealership 2
    [1] => Test Dealership
    [2] => Test Dealership
    [3] => Test Dealership 3
    [4] => Test Dealership
)

分数

Array(
    [0] => 0
    [1] => 427
    [2] => 266
    [3] => 375
    [4] => 180
)

我想做的就是创建一个如下所示的排行榜:

需要合并分数才能匹配经销商。


经销商排行榜

1. 测试经销商 - 873
2. 测试经销商 3 - 375
3. 测试经销商 2 - 0


我已经映射数组来创建这个:

Array
(
    [0] => Array
        (
            [0] => Test Dealership 2
            [1] => 0
        )

    [1] => Array
        (
            [0] => Test Dealership
            [1] => 427
        )

    [2] => Array
        (
            [0] => Test Dealership
            [1] => 266
        )

    [3] => Array
        (
            [0] => Test Dealership 3
            [1] => 375
        )

    [4] => Array
        (
            [0] => Test Dealership
            [1] => 180
        )

)

但我对如何进入下一阶段一无所知。

如果您的数据来自数据库,您可以在 select-查询中处理此问题。

但是给定你的两个数组,只需创建一个新数组,其中键是名称,这样你就可以检查它

$array1=['Test Dealership 2','Test Dealership',...];
$array2=[0,427,...];

$new_array=[];

foreach($array1 as $key => $value){
   //test if KEY (= name) exists in the new array
   //if so, add the value
   if( isset( $new_array[$key] ) ){
      $new_array[$key] += $array2[$key];
      continue;
      }
   //else create a new entry in the new array
   $new_array[ $key] = $array2[$key];
   }

你最终会得到这样的数组:

 [
   'Test Dealership 2' => 0 ,
   'Test Dealership' => 873 ,
   'Test Dealership 3' => 375
]

如果您希望将结果排序为排名列表:

   arsort($new_array);

要得到你想要的字符串,循环 new_array:

$rank=1;
foreach( $new_array as $key => $value ){
   echo $rank.' '.$key.' - '.$value;
   ++$rank;
   }

您可能正在寻找这样的函数:

function reduce_total_scores($input) {
        $output = array_reduce($input, function ($carry, array $current) {
                if (!isset($carry[$current[0]])) $carry[$current[0]] = 0;
                $carry[$current[0]] += $current[1];
                return $carry;
        }, []);
        arsort($output);
        return $output;
}

实际展示:

<?php

$dealerships = [
        'Test Dealership 2',
        'Test Dealership',
        'Test Dealership',
        'Test Dealership 3',
        'Test Dealership',
];

$scores = [
        0,
        427,
        266,
        375,
        180,
];

/**
 * Just a function to create the combined array as the poster did.
 */
function join_array_values(...$arrs) {
        $result = [];
        $l = array_reduce($arrs, function ($carry, array $current) {
                $l = sizeof($current);
                return ($carry >= $l) ? $carry : $l;
        }, 0);
        foreach ($arrs as $i => $arr) {
                $arr = array_values($arr); // only take values
                for ($j=0; $j<$l; $j++) {
                        $result[$j][$i] = $arr[$j] ?? null;
                }
        }
        return $result;
}

/**
 * Reduce the totals of different dealerships
 */
function reduce_total_scores($input) {
        $output = array_reduce($input, function ($carry, array $current) {
                if (!isset($carry[$current[0]])) $carry[$current[0]] = 0;
                $carry[$current[0]] += $current[1];
                return $carry;
        }, []);
        arsort($output);
        return $output;
}

$input = join_array_values($dealerships, $scores);

echo "input\n------\n";
print_r($input);

echo "\nresult\n------\n";
print_r(reduce_total_scores($input));

运行 结果:

input
------
Array
(
    [0] => Array
        (
            [0] => Test Dealership 2
            [1] => 0
        )

    [1] => Array
        (
            [0] => Test Dealership
            [1] => 427
        )

    [2] => Array
        (
            [0] => Test Dealership
            [1] => 266
        )

    [3] => Array
        (
            [0] => Test Dealership 3
            [1] => 375
        )

    [4] => Array
        (
            [0] => Test Dealership
            [1] => 180
        )

)

output
------
Array
(
    [Test Dealership] => 873
    [Test Dealership 3] => 375
    [Test Dealership 2] => 0
)

应该很容易从 reduce_total_scores().

生成的 assoc 数组输出。