0

我有 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
        )

)

但我对如何进入下一阶段持空白。

4

2 回答 2

0

您可能正在寻找这样的功能:

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().

于 2020-06-11T07:33:58.257 回答
0

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

但是鉴于您的两个数组,只需创建一个新数组,其中键是名称,以便您检查它

$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;
   }
于 2020-06-11T07:30:53.267 回答