从多维数组laravel中获取价值

时间:2018-04-17 09:58:41

标签: php laravel

我现在通过在$ temp中推送数据并在$ fullcursus中推送temp来制作一个多维数组但我不知道如何显示特定的数据

public function index()
    {
      $soortcursus =soortcursus::all();
       $cursussen = cursussen::all();
       $fullcursus = array();
foreach ($soortcursus as $soort) {
    foreach ($cursussen as $cursus) {
        if ($soort->soortcode == $cursus->soortcode) {
            $temp = array("cursuscode"=>$cursus->cursuscode,"begindatum"=>$cursus->begindatum,"einddatum"=>$cursus->einddatum,"soortcode"=>$cursus->soortcode,"cursussoort"=>$soort->cursussoort,"prijs"=>$soort->prijs);
           // array_push($fullcursus, $cursus->cursuscode,$cursus->begindatum,$cursus->einddatum,$cursus->soortcode,$soort->cursussoort,$soort->prijs);
        array_push($fullcursus,$temp);
        }
    }
}



       //foreach ($cursuslink as $link) {
       //  array_push($cursussen, cursussen::where('cursuscode',[$link->cursuscode])->get());
     // }

      return view('medewerker_overzicht_cursussen',['cursussen' => $fullcursus]);

    }

$ fullcursus的vardump显示:

array(3) { [0]=> array(6) { ["cursuscode"]=> int(1) ["begindatum"]=> string(10) "2014-06-01" ["einddatum"]=> string(10) "2014-06-08" ["soortcode"]=> int(1) ["cursussoort"]=> string(15) "Beginnerscursus" ["prijs"]=> string(6) "500.00" } [1]=> array(6) { ["cursuscode"]=> int(2) ["begindatum"]=> string(10) "2014-06-08" ["einddatum"]=> string(10) "2014-06-15" ["soortcode"]=> int(1) ["cursussoort"]=> string(15) "Beginnerscursus" ["prijs"]=> string(6) "500.00" } [2]=> array(6) { ["cursuscode"]=> int(3) ["begindatum"]=> string(10) "2014-06-08" ["einddatum"]=> string(10) "2014-06-15" ["soortcode"]=> int(3) ["cursussoort"]=> string(8) "Wadtocht" ["prijs"]=> string(6) "800.00" } }

1 个答案:

答案 0 :(得分:0)

现在你有了一个数组。因为它现在是一个阵列,你可以这样做:

foreach ($fullcursus as $cursus) {
     echo $cursus['cursuscode'];
}

如果您不想使用foreach,则必须先使用数组密钥。如:

echo $cursus[0]['cursuscode'];

最后:

你在这里做的是一种不好的做法。如果与sorscursus的soortcode列与cursussen的soortcode存在关联,则应在此处创建Laravel's Eloquent Relationships