简体   繁体   中英

How to assign one array within another array

i have two array. i just want to merge two array as one array.

public function get_products() 
{
    $products = DB::table('products')->get();

        $arr = array();
        foreach ($products  as $item) 
        {   
            $productImagedata['image'] = DB::table('product_image')-
            >where('product_id', $item->id)->get();
            array_combine( $arr, $productImagedata ); 
        }                

    $pagedata = collect([ "products" => $products ]);
    
    $data = collect(["status" => 
    ["code" => "100", "message" => "Success", "data" =>  $pagedata]]);
    return response()->json($data, 200);
}

in above code. second array should come in first array's value

i just want to like this

 "products": [
            {
                "product_id": 2,
                "product_name": "xyz",
                "image": [
                    {
                        "id": 2,
                        "image_name": "i.jpg",
                    }
                ]
            },

Based on the data from your question, I'm guessing that $item in your loop is equal to

{
    "product_id": 2,
    "product_name": "xyz",
},

In this case, all you need to do is add $productImagedata['image'] as a key in the array, while referencing $item .

Replace your loop like this:

//notice the `&` sign on `$item`. This means we are referencing that variable,
//which basically means that if you change it in the loop, it changes the original as well.
foreach ($products  as &$item) 
{   

    //create image object
    $image_data = DB::table('product_image')->where('product_id', $item->product_id)->get();

    //add image object to `$item` object
    $item->image = $image_data;
} 

The merge method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection:

enter link description here

$collection = collect(['product_id' => 1, 'price' => 100]);

$merged = $collection->merge(['price' => 200, 'discount' => false]);

$merged->all();

// ['product_id' => 1, 'price' => 200, 'discount' => false]

That should work

public function get_products()
{
    $products = DB::table('products')->get();
    $arr = [];
    foreach ($products as $item) {
        $productImageData['image'] = DB::table('product_image')->where('product_id', $item->id)->get()->toArray();
        $arr = array_combine($arr, $productImageData);
    }
    $pageData = collect(['products' => $products]);
    $data = collect(['status' => [
        'code'    => '100',
        'message' => 'Success',
        'data'    => $pageData]]);
    return response()->json($data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM