的foreach函数访问多个变量

问题描述:

我使用Laravel的,我有这样的的foreach函数访问多个变量

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

$ anotherarrayhere的地方一个foreach功能,我想从的foreach功能外这个数组。

$anotherarrayhere = $peopleserviceprice[] 

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

foreach是不是一个函数,你可以使用循环外的变量没有问题。也许你与收集回调混淆,需要具体说明变量

无论如何,你正在遍历模型,而不是集合,因为你使用的是find。

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