2016-05-01 5 views
-1

2つの配列が1つの連想で、2つ目は非連想です。各配列の各項目にデータベースの行として挿入します。以下のコードでは、$ menuと$ idは配列です。この$ id部分は、それ自身が配列であるので私にエラーを与えています。エラーは "配列から文字列への変換"と言います。助けてください!!!foreachループで2つの配列を反復して単一のレコードを取得する方法

foreach($menu as $label => $link) { 
    $id = $request->themeLocation; 
    DB::table('menus')->insertGetId([ 'label' => $label ,'url' => $link ,'themeLocation' => '$id ,'menu_status' => 1 ]); 
} 

foreachの中で、私はDB

id themeLocation url label menu_status 

     41 child-38 about ABOUT US 1 
     42 child-39 about ABOUT US 1 
     43 child-40 about ABOUT US 1 
     44 child-38 services Services 1 
     45 child-39 services Services 1 
     46 child-40 services Services 1 
     47 child-38 contact contact 1 
     48 child-39 contact contact 1 
     49 child-40 contact contact 1 

における階層エントリをgetingですが、私は、DB内の冗長エントリを取得していますとのforeachを適用した後、私はこの結果に

 41 child-38 about ABOUT US 1 
     42 child-39 services Services 1 
     43 child-40 contact contact 1 

感謝をしたいです!

+0

構文の強調表示はおそらくあなたの問題を解決しますか? –

+0

'$ id'の前に余分な引用符があります。 – Barmar

+0

ありがとう、どのように??? –

答えて

0

構文エラーは、=> '$idに余分な引用があったためです。

$idが配列の場合は、すべての値を挿入するためにネストループが必要です。配列内の何ものにも依存しないので、配列は$menuループの外側になければなりません。

$locations = $request->themeLocation; 
foreach ($menu as $label => $link) { 
    foreach ($locations as $loc) { 
     DB::table('menus')->insertGetId([ 
      'label' => $label , 
      'url' => $link , 
      'themeLocation' => $loc, 
      'menu_status' => 1 ]); 
    } 
} 
+0

私はそれが何を意味するのか分かりません。 – Barmar

関連する問題