Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 生成重复项的laravel嵌套foreach循环_Php_Laravel_Laravel 5.2 - Fatal编程技术网

Php 生成重复项的laravel嵌套foreach循环

Php 生成重复项的laravel嵌套foreach循环,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,此方法返回json响应。每个订单都有许多产品,我希望数据数组中的产品密钥具有与该订单相关的产品名称 $customerData = Order::whereAdminId($this->admin->id)->whereCustomerId($customer->id)-> whereBetween('created_at', array($date_from,$date_to))->get(); if (count($customerData) >

此方法返回json响应。每个订单都有许多产品,我希望数据数组中的产品密钥具有与该订单相关的产品名称

$customerData = Order::whereAdminId($this->admin->id)->whereCustomerId($customer->id)->
whereBetween('created_at', array($date_from,$date_to))->get();

if (count($customerData) > 0) {
    $data1 = array();
    $data3 = array();
    foreach ($customerData as $value) {
        foreach ($value->orderProducts as $value1) {            
            $data3[] = array($value1->products($value1->product_id)->name);
        }
        $data[] = array(
            'product'=>$data3, 
        );
    }
    return response()->json(['success'=>true, 'customerData'=>$data], 200);
}

但在嵌套的foreach上,以前的值会添加到新值,并且每个订单的所有产品名称都会重复。谢谢

您是否在
$data3
分配上尝试了
array_push()
是重复了$data?此方法在返回对象时在post模式下搜索客户订单它在刀片模板上正常工作。但要为ajax响应创建数组,请复制值