Home > Backend Development > PHP Tutorial > There is the following data in the excel file. How to do data statistics without using a database?

There is the following data in the excel file. How to do data statistics without using a database?

WBOY
Release: 2016-08-18 09:16:14
Original
1469 people have browsed it

id band (brand) num (sales quantity) price (unit price)
1 Samsung 7677 2300
2 Huawei 9877 1699
3 Apple 24552 6799
... More brands, unknown quantity, consider the large quantity in excel

Question:
If we want to count the top 3 brands with the largest total sales (the database cannot be used)
just tell me your idea, thank you

Reply content:

id band (brand) num (sales quantity) price (unit price)
1 Samsung 7677 2300
2 Huawei 9877 1699
3 Apple 24552 6799
... More brands, unknown quantity, consider the large quantity in excel

Question:
If we want to count the top 3 brands with the largest total sales (the database cannot be used)
just tell me your idea, thank you

php gets the execl content. Each cell will be a key in the array. Sales (quantity * price) are accumulated according to the brand key, and the top three are finally sorted.

Thank you for the invitation. You can use a program to read the excel file and organize it into an array, and then sort the array by price. Personal understanding

Excel problem?


  • Create a new column and calculate the total sales of each row;

  • Enable filtering

  • Sort


PHP implementation method:

<code class="php"><?php 
$data   = []; // excel的数据 brand: 品牌; price: 价格; qty: 销量;

$result = [];

foreach ($data as $item) {
    $total = $item['price'] * $item['qty'];

    if (array_key_exists($item['brand'], $result)) {
        $result[$item['brand']] += $item['price'] * $item['qty'];
    } else {
        $result[$item['brand']] = $total;
    }
}

arsort($result);  // 值降序排序

// 前三个值的key就是品牌了
?>
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template