【问题标题】:foreach function accessing multiple variablesforeach 函数访问多个变量
【发布时间】:2018-04-02 15:03:36
【问题描述】:

我正在使用 Laravel,并且我有一个这样的 foreach 函数

$name = person::find(1);
foreach($name as $dbitems)  {
            $person->services()->updateOrCreate([
                'service' => $dbitems->Name,
                'time' => $dbitems->Time
                'price' => $anotherarrayhere
                ]);
        }

$anotherarrayhere 的地方,我想从 foreach 函数的外部获取这个数组。

$anotherarrayhere = $peopleserviceprice[]

如何在上述 foreach 函数中包含 $anotherarrayhere 变量?

【问题讨论】:

    标签: php arrays laravel foreach


    【解决方案1】:

    foreach 不是函数,您可以使用循环外部的变量而不会出现问题。也许你对需要特定告诉变量的集合回调感到困惑

    无论如何,您是在迭代一个模型,而不是一个集合,因为您使用的是 find。

    $person = person::find(1);
    $person->services()->updateOrCreate([
        'service' => $person->name,
        'time' => $person->time
        'price' => $anotherarrayhere
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多