PHP:如何从数据库处理这些数据

时间:2016-02-05 07:34:59

标签: php

我对编程很陌生,所以我很抱歉,如果这是非常高兴的话。

在虚拟数据库中,我有一个名为currency的列。该列包含以下数据:

36:USD,74:GBP,68:USD,119:USD,114:BGN,15:USD,32:GBP,1:BGN

以上数据是用户已支付的金额及其各自的货币。

我想要做的是显示包含每个货币的表格以及该国家/地区的总销售额,并以表格形式显示。

因此,根据以上数据,我想要下面的输出:

Currency  |  # Sales
____________________

USD         4
GPB         2
BGN         2 
____________________

     Total: 8

3 个答案:

答案 0 :(得分:1)

试试这个

$data = "36:USD,74:GBP,68:USD,119:USD,114:BGN,15:USD,32:GBP,1:BGN";
$data = explode(",", $data);

$result = array();
foreach ($data as $value) {
    $d_array = explode(":", $value);
    if(isset($result[$d_array[1]]))
        $result[$d_array[1]] +=1;
    else
        $result[$d_array[1]] = 1;
}
$total = count($data);

var_dump($result);
var_dump($total);

结果

array (size=3)
'USD' => int 4
'GBP' => int 2
'BGN' => int 2

int 8

答案 1 :(得分:1)

$data = "36:USD,74:GBP,68:USD,119:USD,114:BGN,15:USD,32:GBP,1:BGN";
$data = explode(",", $data);
$final_array = array();
foreach ($data as $value) 
{
   $new_array = explode(":", $value);
   array_push($final_array, $new_array[1]);
}
$final_array = array_count_values($final_array);
print_r($final_array);

结果

数组([美元] => 4 [英镑] => 2 [BGN] => 2)

答案 2 :(得分:0)

看看我的例子。我希望对你好。

$orders = array("36:USD","74:GBP","68:USD","119:USD","114:BGN","15:USD","32:GBP","1:BGN");

$totalData = array();
foreach($orders as $singleOrder) {
    $explodedData = explode(':',$singleOrder);

    if(!isset($totalData[$explodedData[1]])) {
        $totalData[$explodedData[1]] = $explodedData[0]; 
    } else {
        $totalData[$explodedData[1]] += $explodedData[0]; 
    }
}

var_dump($totalData);

结果

array (size=3)
  'USD' => int 238
  'GBP' => int 106
  'BGN' => int 115