阵列数据问题Laravel

时间:2017-06-10 09:23:24

标签: php arrays laravel laravel-5

我在这里遇到阵列问题而且我花了很多时间。 我得到一个像这种形式的数组列表

array:4 [

0 => array:20 [▼
"id" => 58
"id_business" => 
"id_branch" => 
"affected_branches" => " " ▶"
"name" => ""
"banner" => ""
"description" => ""
"url" => 
"start_date" => ""
"end_date" => ""
"nr_pages" => 4
"nr_products" => 0
"type" => "global"
"views" => 24
"visible" => "yes"
"delete" => "no"
"user_create_id" => 
"user_modify_id" => 
"created_at" => ""
"updated_at" => "2017-06-07 14:19:23"]
]

通过以下代码

  $data = Offers::whereIn('id_business', $business_id_array)
                    ->where([
                       'visible' => 'yes',
                       'delete' => 'no'
                     ])
                     ->where('end_date', '>=', date('Y-m-d h:m:i'))
                    ->orderBy('id', 'desc')->get()
                    ->toArray();

当我尝试为所有出现的数组创建一个foreach循环时,在这种情况下为4,它不会返回4个值,而只返回一个值。 循环是:

 foreach ($data as $key => $slide)
                {
                    $offers = DB::select('SELECT o.`id`... WHERE o.`id` = ?',[$slide['id']]);

                }

                return view('...', ['offers' => $offers]);
            }

我在查询中传递此值以获取每个数组的id

o.`id` = ?',[$slide['id']]

PS:PLS帮助我:)

2 个答案:

答案 0 :(得分:2)

您每次迭代都会覆盖$offers值,因此请将代码更改为:

$offers = [];
foreach ($data as $key => $slide) {
    $offers[] = DB::select...
}

答案 1 :(得分:1)

您需要将$ offres更改为数组

$offers = array();
foreach ($data as $key => $slide)
{
    $offers[] = DB::select('SELECT o.`id`... WHERE o.`id` = ?',[$slide['id']]);

}

return view('...', ['offers' => $offers]);