Laravel中的JSON元素的人工操作

发布于 2025-02-11 07:15:18 字数 667 浏览 1 评论 0原文

我尝试使用JSON文件进行一些操作。

例如,我打算减去它们。但是问题在于可能有不同的关键要素。

         $a1=[
            {"cat":2,"total":1},
            {"cat":11,"total":23},
            {"cat":13,"total":30}
        ];

        $a2=[
            {"cat":2,"total":15},
            {"cat":11,"total":13},
            {"cat":15,"total":70},
            {"cat":16,"total":40}
        ];

结果一定是

        $result=[
            {"cat":2,"total":14},
            {"cat":11,"total":-10},
            {"cat":13,"total":-30},
            {"cat":15,"total":70},
            {"cat":16,"total":40}
        ]

我试图在循环中采用元素,但我不能。

您能告诉我做这项工作的方式吗?

I try to make some operations with JSON files.

For example I intend to subtract them. But the problem is there can be different key elements.

         $a1=[
            {"cat":2,"total":1},
            {"cat":11,"total":23},
            {"cat":13,"total":30}
        ];

        $a2=[
            {"cat":2,"total":15},
            {"cat":11,"total":13},
            {"cat":15,"total":70},
            {"cat":16,"total":40}
        ];

and result must be

        $result=[
            {"cat":2,"total":14},
            {"cat":11,"total":-10},
            {"cat":13,"total":-30},
            {"cat":15,"total":70},
            {"cat":16,"total":40}
        ]

I have tried to take elements in a loop but I could not.

would you please show me the way to make this work ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2025-02-18 07:15:18

首先,代码:

<?php

$json1 = '[{"cat":2,"total":1},{"cat":11,"total":23},{"cat":13,"total":30}]';
$json2 = '[{"cat":2,"total":15},{"cat":11,"total":13},{"cat":15,"total":70},{"cat":16,"total":40}]';

$first = json_decode($json1, true);
$second = json_decode($json2, true);

$difference = [];

foreach ($second as $s) {
    $key = null;
    $f = array_filter($first, function($v, $k) use ($s, &$key){
        $result = $v["cat"] === $s["cat"];
        if ($result) $key = $k;
        return $result;
    }, ARRAY_FILTER_USE_BOTH);
    $total = $s["total"] - (count($f) ? $f[$key]["total"] : 0);
    $difference[]=["cat" => $s["cat"], "total" => $total];
}

foreach ($first as $f) {
    $s = array_filter($second, function($v, $k) use ($f) {
        return $v["cat"] === $f["cat"];
    }, ARRAY_FILTER_USE_BOTH);
    if (!count($s)) {
        $difference[]=["cat" => $f["cat"], $total => -$f["total"]];
    }
}

echo var_dump($difference); //I tested by echoing it out

说明:

  • 我使用您定义的JSON输入初始化两个变量
  • 。将是关联阵列,
  • 我循环第二
    • 我用null初始化键
    • 我在第一次使用$ s$ key中搜索匹配
      • 如果我找到了这样的匹配项,则我用它初始化$ key
    • 我从总数中减去总数(如果存在)
    • 我添加了一个与匹配类别的新项目和正确的新总计
    • 我使用$ f
    • 在第二次搜索比赛

  • 如果找不到匹配项,则我将项目添加为匹配类别,

简而默认为0,然后我第一个循环添加第二个没有匹配的元素。

First, the code:

<?php

$json1 = '[{"cat":2,"total":1},{"cat":11,"total":23},{"cat":13,"total":30}]';
$json2 = '[{"cat":2,"total":15},{"cat":11,"total":13},{"cat":15,"total":70},{"cat":16,"total":40}]';

$first = json_decode($json1, true);
$second = json_decode($json2, true);

$difference = [];

foreach ($second as $s) {
    $key = null;
    $f = array_filter($first, function($v, $k) use ($s, &$key){
        $result = $v["cat"] === $s["cat"];
        if ($result) $key = $k;
        return $result;
    }, ARRAY_FILTER_USE_BOTH);
    $total = $s["total"] - (count($f) ? $f[$key]["total"] : 0);
    $difference[]=["cat" => $s["cat"], "total" => $total];
}

foreach ($first as $f) {
    $s = array_filter($second, function($v, $k) use ($f) {
        return $v["cat"] === $f["cat"];
    }, ARRAY_FILTER_USE_BOTH);
    if (!count($s)) {
        $difference[]=["cat" => $f["cat"], $total => -$f["total"]];
    }
}

echo var_dump($difference); //I tested by echoing it out

Explanation:

  • I initialize two variables with the JSON inputs you have defined
  • I decode them both using json_decode and I set the second parameter to true to make sure they will be associative arrays
  • I loop the second
    • I initialize the key with null
    • I search for a match in the first using $s and $key so I will see these variables in the closure of the function
      • If I find such a match then I initialize $key with it
    • I subtract the first total (if exists) from the total
    • I add a new item with the matching category and the correct new total
  • I loop the first
    • I search a match in the second using $f
  • If no matches were found, I add the item with the matching category and negative total

In short, I loop the second and subtract the matching first if they exist, defaulting to 0 and then I loop the first to add the elements that do not have a match in the second.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文