问题描述
我在控制器中有两个变量:
I have two variables in a controller:
$data = S_core_Country::all();
$cn=\DB::table('s_user_addresses')
->join('s_users', 's_users.id', '=', 's_user_addresses.user_id')
->join('s_core_countries', 's_core_countries.id', '=', 's_user_addresses.country_id')
->value('countryname');
return view('home')->with(['data'=>$data,'cn'=>$cn]);
是否可以像这样在foreach循环中查看我的数据
Is it possible to view my data in foreach loop like this
@foreach($data as $data && $cn as $cn)
如果不是,我应该在一个foreach循环中显示那两个变量吗?
If not what should I to show those two variable in one foreach loop?
推荐答案
要合并和合并数组,可以尝试:
For merge and combine array , you can try :
$array = array_merge($array1,$array2);
刀片中的示例:
@foreach(array_merge($array1,$array2) as $item)
// ...
@endforeach
另一个例子:
Another Example :
@foreach($array1 as $key => $row)
<li>
<b>{{ $row['id'] }}</b>
<p>{{ $array2[$key]->payment }}</p>
</li>
@endforeach
相关链接:
Related Links :
https://codereview.stackexchange.com/Questions/70419/使用嵌套的foreach循环合并两个数组
https://knackforge.com/blog/sabareesh/iterate-2-arrays-single-foreach-loop
http://php.net/manual/zh/function.array-merge.php
https://www.w3schools.com/php/func_array_merge.asp
https://www.w3schools.com/php/showphp.asp?filename = demo_func_array_merge
https://laravel.com/docs/5.7/blade#the-loop-variable
&&运算符(和)是逻辑运算符.
The && operator(and) is a logical operator.
-
http://php.net/manual/zh/language.operators.ologic.php
https://laravel-news.com/blade-or-operator
这篇关于laravel刀片中的一个Foreach循环中的多个变量传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!