3 回答

TA貢獻1828條經驗 獲得超3個贊
您不能在數組語法中使用循環。解決的辦法是在外面構建數組,然后作為參數傳遞。我不確定您正在尋找的確切結構,但類似于這樣:
$data = [];
for ($z = 0; $z < 8; $z++) {
$data[] = ($masterCategoryList[$z]['name'] . $productMatchesToMasterCategory);
}
return view('shop.landing', [
'productMatchesToMasterCategory' => $data,
'tomorrow' => Carbon::tomorrow(),
]);

TA貢獻1829條經驗 獲得超13個贊
您應該在返回視圖之前執行此操作:
$productMatchesToMasterCategoryArray = [];
for($z = 0; $z < 8; $z++) {
$productMatchesToMasterCategoryArray[] = ($masterCategoryList[$z]['name'].$productMatchesToMasterCategory);
}
return view('shop.landing', [
'productMatchesToMasterCategory' => $productMatchesToMasterCategoryArray
//Other variables here
]);
希望能幫助到你。

TA貢獻1852條經驗 獲得超1個贊
您可以嘗試在返回視圖之前獲取隨機 8 個元素的數組。
$productMatchesToMasterCategoryArray = [];
foreach($masterCategoryList as $z){
$productMatchesToMasterCategoryArray[] = ($z['name'].$productMatchesToMasterCategory);
}
$random_Array=array_rand($productMatchesToMasterCategoryArray,8);
return view('shop.landing', [
'productMatchesToMasterCategory' => $random_Array,
'tomorrow' => Carbon::tomorrow(),
]);
- 3 回答
- 0 關注
- 230 瀏覽
添加回答
舉報