【问题标题】:How to update main array in laravel如何更新laravel中的主数组
【发布时间】:2019-07-18 09:00:51
【问题描述】:

我需要帮助更新 Main Array 并在 foreach 循环中更改其数据。 这是我的代码:

$query = DB::table('autostk')
    ->where('autostk.branchid', $branch_id)
    ->where('autostk.itemcode', $request->itemcode)
    ->whereDate('autostk.date', '<=', $request->tdate)
    ->where('autostk.branchid', $branch_id)
    ->leftjoin('journal', 'autostk.refno', '=', 'journal.vno')
    ->where('journal.code', '>=', 100)
    ->where('journal.branchid', $branch_id)
    ->leftjoin('accounts', 'journal.code', '=', 'accounts.code')
    ->where('accounts.branchid', $branch_id)
    ->select('journal.code', 'accounts.title', 'autostk.*')
    ->orderBY('date')->get()
    ->map(function ($item, $key) {
        return (array)$item;
    })
    ->all();

foreach ($query as $row) {
    if (is_null($row['qtyin'])) {
        $row['qtyin'] = 0;
    }
    if (is_null($row['qtyout'])) {
        $row['qtyout'] = 0;
    }
    if (is_null($row['rate'])) {
        $row['rate'] = 0;
    }
    if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
        $stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
        if ($bal > 0) {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            if ($bal > 0 && $stkval > 0) {
                $avgrate = $stkval / $bal;
            }
        } else {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            $avgrate = $row['rate'];
        }
    } else {
        $bal = $bal + $row['qtyin'] - $row['qtyout'];
    }
    $row['balqty'] = $bal;
    $row['avgrate'] = $avgrate;
}

我的问题是如何使用对 $row 所做的更改来更新 $query。我是 php 和 laravel 的新手,尝试过 push()、put() 等。不知道在这种情况下需要哪个函数。

【问题讨论】:

  • 如果要向数组添加新值,则可以使用 push。你能详细解释一下你的问题吗?

标签: arrays laravel laravel-5.8


【解决方案1】:

要将您应用到 $row 的更改保留在 foreach 中,您只需通过引用传递它:

foreach ($query as &$row) { //notice the & before $row 

或者,您可以将foreach 中的代码移动到map 闭包中:

->map(function ($item, $key) use ($bal, $avgrate) {

    $row = (array)$item;

    if (is_null($row['qtyin'])) {
        $row['qtyin'] = 0;
    }
    if (is_null($row['qtyout'])) {
        $row['qtyout'] = 0;
    }
    if (is_null($row['rate'])) {
        $row['rate'] = 0;
    }
    if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
        $stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
        if ($bal > 0) {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            if ($bal > 0 && $stkval > 0) {
                $avgrate = $stkval / $bal;
            }
        } else {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            $avgrate = $row['rate'];
        }
    } else {
        $bal = $bal + $row['qtyin'] - $row['qtyout'];
    }
    $row['balqty'] = $bal;
    $row['avgrate'] = $avgrate;

    return $row;

}) 

【讨论】:

  • 非常感谢。 foreach ($query as &$row) 效果很好。
【解决方案2】:

试试下面的代码:

$query = DB::table('autostk')
    ->where('autostk.branchid', $branch_id)
    ->where('autostk.itemcode', $request->itemcode)
    ->whereDate('autostk.date', '<=', $request->tdate)
    ->where('autostk.branchid', $branch_id)
    ->leftjoin('journal', 'autostk.refno', '=', 'journal.vno')
    ->where('journal.code', '>=', 100)
    ->where('journal.branchid', $branch_id)
    ->leftjoin('accounts', 'journal.code', '=', 'accounts.code')
    ->where('accounts.branchid', $branch_id)
    ->select('journal.code', 'accounts.title', 'autostk.*')
    ->orderBY('date')->get()
    ->map(function ($item, $key) {
        return (array)$item;
    })
    ->all();

$row_data = [];
foreach ($query as $row) {
    if (is_null($row['qtyin'])) {
        $row['qtyin'] = 0;
    }
    if (is_null($row['qtyout'])) {
        $row['qtyout'] = 0;
    }
    if (is_null($row['rate'])) {
        $row['rate'] = 0;
    }
    if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
        $stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
        if ($bal > 0) {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            if ($bal > 0 && $stkval > 0) {
                $avgrate = $stkval / $bal;
            }
        } else {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            $avgrate = $row['rate'];
        }
    } else {
        $bal = $bal + $row['qtyin'] - $row['qtyout'];
    }
    $row['balqty'] = $bal;
    $row['avgrate'] = $avgrate;
    $row_data[] = $row;
}

现在使用$row_data

【讨论】:

    【解决方案3】:

    选项 1

    //use toArray() in last to get the result as an array
    $query = ...->all()->toArray();
    
    foreach ($query as $key => $row) {
         //inside here instead of using $row, use $query[$key]
         //so for example $row['rate'] = 0; becomes:
         $query[$key]['rate'] = 0;
    }
    

    选项 2

    //use toArray() in last to get the result as an array
    $query = ...->all()->toArray();
    
    //use pass by reference with help of &
    foreach ($query as $key => &$row) {
       ...
    }
    

    但是,使用引用传递方法时要非常小心,否则如果重复使用相同的数组,可能会遇到问题。

    选项 3

    $query = ...->all();
    foreach ($query as $key => $row) {
        //access it as object
        //instead of using $row['qtyin'] use:
        $row->qtyin = 0;
    }
    

    经销商的选择:)

    【讨论】:

      猜你喜欢
      • 2017-06-10
      • 2020-08-19
      • 2020-01-25
      • 2019-10-15
      • 2017-01-07
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多