如何在Laravel 5.4的foreach循環外使用數組值?
下面是代碼:
如何在foreach循環之外使用數組值?
public function index(Request $request)
{
$name = $request->input('keyword');
$category = $request->input('category');
$catkeywords = array(DB::table('keywords')->pluck($category));
foreach ($catkeywords as $catkeyword) {
$string = implode(',',$catkeyword);
}
echo $string;
}
我不知道爲什麼它不工作!
我只是想從數據庫中返回的關鍵字組合他們提供一些文字,並使用一段API查詢。
換句話說,我想要在循環之外的關鍵字列表。
對於使用在這樣一個API查詢:
http://api-url/query?id=domain1.com,domain2.com,domain3.com
$catkeywords
返回關鍵字JSON格式名單。
現在我想將這些關鍵詞與用戶輸入的值和添加一個「.COM」後綴, 然後將它們分開使用逗號,並利用它們對查詢網址作爲變量。
P.S:我正在使用guzzlehttp
向API發送請求。因此,它應放在:
'DomainList' => $domainlist
我怎麼能這樣做?
'$ string'你'的foreach loop'內將更換其第v在每個循環中都有。所以,你需要使用'。='連接。在等於之前記住(點)。 – vijayrana
是否返回任何錯誤信息 – ashanrupasinghe