【问题标题】:Differentiate Array base on Value And Sum Of Array in Laravel基于Laravel中数组的值和总和来区分数组
【发布时间】:2019-05-06 16:51:31
【问题描述】:

我有数组,首先我想根据站点(键)来区分,站点不限于 1 或 2 然后是网站的期初余额和期末余额之和 像所有网站(888)total_balance = opening_balance + closing_balance (999) total_balance = opening_balance + closing_balance

"games": [
    {
        "id": 240,
        "user_id": 1,
        "site": "888",
        "opening_balance": 5,
        "closing_balance": 6
    },
    {
        "id": 243,
        "user_id": 1,
        "site": "999",
        "opening_balance": 3,
        "closing_balance": 4
    },
    {
        "id": 244,
        "user_id": 1,
        "site": "888",
        "opening_balance": 5,
        "closing_balance": 6
    }, 

并且想要像这样的输出 (站点 => 888,total_balance => 22,站点 => 999,total_balance => 7 (可以是字符串))

我试过的代码:

{
    $collection = collect(Site::all())->map(function($item , $key){
                            return $item["name"];
                        });

    $record = Game::whereIn('site',$collection)->get();
    dd($record);

    /*$bySite = array();
        foreach ($record as $key => $item) {
            if(!isset($bySite[$item['site']])) {
                $bySite[$item['site']] = array();
            }
            $bySite[$item['site']][$key] = $item;
        }*/
}

在我注释输出的这段代码之后 但是我不知道这个总和是多少

{
"888": {
    "0": {
        "id": 240,
        "user_id": 1,
        "site": "888",
        "opening_balance": 5,
        "closing_balance": 6
    },
   "2": {
        "id": 244,
        "user_id": 1,
        "site": "888",
        "opening_balance": 5,
        "closing_balance": 6
    },
  },
"999": {
    "1": {
        "id": 243,
        "user_id": 1,
        "site": "999",
        "opening_balance": 3,
        "closing_balance": 4,

【问题讨论】:

  • 您需要在所有项目中包含total_balance 或者您需要按site 分组并包含total_balance?你能更具体地描述你想要的输出吗?
  • 是,先生按站点分组,然后包括总余额
  • 你能指定你期望的输出吗?请编辑并指定您的输出结构。
  • required = site => 888, total_balance => 22 site => 999, total_balance => 7 这可能是字符串我没有问题只是我需要这个输出....我需要所有的总余额站点 888 独立和 999 独立等等 (111)

标签: php laravel laravel-5 laravel-4


【解决方案1】:

你可以使用 Laravel Collection 来实现。

$games =  [
    [
        "id" =>  240,
        "user_id" =>  1,
        "site" =>  "888",
        "opening_balance" =>  5,
        "closing_balance" =>  6
    ],
    [
        "id" =>  243,
        "user_id" =>  1,
        "site" =>  "999",
        "opening_balance" =>  3,
        "closing_balance" =>  4
    ],
    [
        "id" =>  244,
        "user_id" =>  1,
        "site" =>  "888",
        "opening_balance" =>  5,
        "closing_balance" =>  6
    ]
];

$games = collect($games);

$games = $games->groupBy('site')->map(function($game, $key){
    return [
        'site' => $key,
        'total_balance' => $game->sum('opening_balance') + $game->sum('closing_balance'),
    ];
})->values();

dd($games->toArray());

输出会是,

array:2 [▼
    0 => array:2 [▼
        "site" => 888
        "total_balance" => 22
    ]
    1 => array:2 [▼
        "site" => 999
        "total_balance" => 7
    ]
]

代码片段:https://implode.io/5aPGeH

【讨论】:

  • 非常感谢先生您的回答真的很有价值......但我早上用另一种方式做到了。
  • 不客气。如果这解决了您的问题,您可以接受答案。
【解决方案2】:

步骤 1. foreach ($collection) 2. 对值求和并存储在数组中,例如 ($collection->sum('opening_balance'), ($collection->sum('closing_balance') 3.然后对变量求和 4.停止循环 5. 然后 $final = $final['site'] 。 “=>”。 $final['sum'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2021-11-20
    相关资源
    最近更新 更多