如何解码json,按列值分组以及每个组中的总和值? [英] How to decode json, group by a column value, and sum values in each group?

查看:84
本文介绍了如何解码json,按列值分组以及每个组中的总和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有数据.该数据由json数据创建.但是我不能从数据键更改为数组.我想按库存数量和顺序设置此数据.

I have data in MySQL. The data is created by json datas. But I can't change to array from data keys. I want to set this data in order and according to the number of stock.

[
{"size":"36","stock":"1"},
{"size":"37","stock":"2"},
{"size":"38","stock":"1"},
{"size":"40","stock":"1"},
{"size":"36","stock":"1"},
{"size":"37","stock":"3"},
{"size":"38","stock":"2"},
{"size":"39","stock":"3"},
{"size":"40","stock":"2"}
]

我想更改为:

array(
'36' => '2',
'37' => '5',
'38' => '3',
'39' => '3',
'40' => '3',
)

我编写了此函数,但只返回了true,我觉得它可能会更完善:

I wrote this function but it only returns true and I feel it could be more refined:

function shoesizes($json,$key='size')
{
    $array =  json_decode($json);
    $result = array();
    $sum = 0;
    $i=0;
    foreach((array) $array as $val) {
        if(array_key_exists($key, $val)){ 
            $result[$val->$key][] = (array)$val;  
        }else{
            $result[""][] = $val;
        }
    }   
    $arrsi = array(); 
    foreach ($result as $k => $v) { 
        $sum = 0; 
        foreach ($v as $c => $d) { 
            $sum +=     $d['stock']; 
        }
        $arrsi[$k]= $sum; 
    }
    return ksort($arrsi);

}

推荐答案

解码然后迭代数组.如果第一次出现size,则存储整数值;否则,将新值添加到存储的值中.完成后,按结果数组键排序.

Decode then iterate the array. If the first occurrence of size store the integer value, if not add the new value to the stored value. When done, sort by the result array keys.

代码:( 演示)

$json = '[
{"size":"36","stock":"1"},
{"size":"37","stock":"2"},
{"size":"38","stock":"1"},
{"size":"40","stock":"1"},
{"size":"36","stock":"1"},
{"size":"37","stock":"3"},
{"size":"38","stock":"2"},
{"size":"39","stock":"3"},
{"size":"40","stock":"2"}
]';

$array = json_decode($json, true);
foreach ($array as $row) {
    if (isset($result[$row['size']])) {
        $result[$row['size']] += $row['stock'];
    } else {
        $result[$row['size']] = (int)$row['stock'];
    }
}
ksort($result);
var_export($result);

输出:

array (
  36 => 2,
  37 => 5,
  38 => 3,
  39 => 3,
  40 => 3,
)


代码审查:


Code Review:

  • ksort()返回布尔结果.通过编写return ksort($arrsi);,您只能接收truefalse(并且false仅在发生故障时可以发生). http://php.net/manual/zh/function.ksort.php分别编写ksort($arrsi);return $arrsi;之后,函数将返回所需的结果.
  • 您声明但不使用$i,以便可以安全地删除该行.
  • 不需要$sum = 0;的第一个声明,因为稍后在第二个循环中重新声明了变量.
  • 您的第一个循环会创建不必要的肿数据结构.将size值分配为键当然是正确的步骤.将每个日期集存储为新密钥的子数组比您所需要的更多.这导致您必须遵循嵌套循环.
  • ksort() returns a boolean result. By writing return ksort($arrsi);, you can only receive true or false (and false can only happen when there is a failure). http://php.net/manual/en/function.ksort.php Once you write ksort($arrsi); then return $arrsi; then your function returns the desired result.
  • You declare, but don't use $i so that line can be safely removed.
  • The first declaration of $sum = 0; isn't necessary because you redeclare the variable later in your second loop.
  • Your first loop creates an unnecessarily bloated data structure. Assigning the size values as keys is certainly the right step. Storing each set of date as a subarray of the new key is more than what you need. This causes you to have to follow up with a nested loop.

这篇关于如何解码json,按列值分组以及每个组中的总和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆