dongxianshuai8927
2017-05-15 19:37
浏览 56
已采纳

如何在laravel刀片中传递多维数组?

I have try to pass the multidimensional array to view. But when I try to print It will give me error. Below are the code and error.

Controller File:

foreach($countrygroup as $val){
        //$userArr[] = $val->userid;
        $users =  DB::table('timelines')->where('id', '=', $val->userid)->first();
        $userArr[$val->userid]['name'] = $users->name;//$users->name;
        $userArr[$val->userid]['avatar'] = $users->avatar_id;
        //$userArr[$val->userid]['image']=$users->image;            
    }
    //echo '<pre>'; print_r($userArr); die();
      return $theme->scope('groups/country', compact('userArr','trending_tags','countrygroupnamee'))
      ->render();

Blade File Code:

@foreach($userArr as $users)
    <h2>{{ $users}}</h2>                                                                
 @endforeach

enter image description here

Thanks

图片转代码服务由CSDN问答提供 功能建议

我尝试传递多维数组进行查看。 但是当我尝试打印它会给我错误。 下面是代码和错误。

控制器文件:

  foreach($ countrygroup as $ val){
 // $ userArr  [] = $ val-&gt; userid; 
 $ users = DB :: table('timelines') - &gt; where('id','=',$ val-&gt; userid) - &gt; first()  ; 
 $ userArr [$ val-&gt; userid] ['name'] = $ users-&gt; name; // $ users-&gt; name; 
 $ userArr [$ val-&gt; userid] ['avatar  '] = $ users-&gt; avatar_id; 
 // $ userArr [$ val-&gt; userid] ['image'] = $ users-&gt; image;  
} 
 // echo'&lt; pre&gt;'; 的print_r($ userArr);  die(); 
返回$ theme-&gt;范围('groups / country',compact('userArr','trending_tags','countrygroupnamee'))
  - &gt; render(); 
    
 
 

刀片文件代码:

  @foreach($ userArr as $ users)
&lt; h2&gt; {{$ users}}  &LT; / H2&GT;  
 @endforeach 
   
 
 

< img src =“https://i.stack.imgur.com/2LjqI.png”alt =“在此处输入图像说明”>

谢谢 \ n

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhanlie9209 2017-05-15 19:39
    已采纳

    The blade template tags {{ }} are replaced with an echo(). You can't echo an array. You need to get the string out of the array that you want to display, for example:

    @foreach($userArr as $users)
        <h2>{{ $users['name'] }}</h2>                                                                
    @endforeach
    
    点赞 评论

相关推荐 更多相似问题