gpt4 book ai didi

php - 拉维尔 : How do I add key and value into Collection or array

转载 作者:行者123 更新时间:2023-12-05 02:58:01 25 4
gpt4 key购买 nike

我从数据库中获取了一个集合,我可以使用 toarray() 更改类型,但我不知道如何将键和值添加到集合或数组中

这是我的收藏

Collection {#225 ▼
#items: array:1 [▼
0 => {#222 ▼
+"product_id": 48
+"product_name": "xxxxxxx"
+"product_number": 400
+"product_price": 300
+"product_describe": "qqqqqqqqqq"
+"product_status_id": 1
+"product_category_id": 1
+"product_buy_price": 200
+"created_at": null
+"updated_at": null
}
]
}

这是我的数组

array:1 [▼
0 => {#222 ▼
+"product_id": 48
+"product_name": "xxxxxxx"
+"product_number": 400
+"product_price": 300
+"product_describe": "qqqqqqqqqq"
+"product_status_id": 1
+"product_category_id": 1
+"product_buy_price": 200
+"created_at": null
+"updated_at": null
}
]

我想添加到数组键和值。

"buynumber" => "$buynumber"

我尝试使用合并。 Controller .php

        $buynumber = Input::get('buynumber');
$product_id = Input::get('product_id');
$product = DB::table('product')
->where('product_id', $product_id)
->get();

$product_array = $product->toArray();
$buy_number = array('buynumber' => $buynumber);
$merge = array_merge($product_array, $buy_number);
return View('shop/cart')
->with('merge',$merge);

这不是我想要的。

合并结果

array:2 [▼
0 => {#222 ▼
+"product_id": 48
+"product_name": "xxxxxxx"
+"product_number": 400
+"product_price": 300
+"product_describe": "qqqqqqqqqq"
+"product_status_id": 1
+"product_category_id": 1
+"product_buy_price": 200
+"created_at": null
+"updated_at": null
}
"buynumber" => "5"
]

我怎样才能得到一个数组而不是两个数组?

最佳答案

如果您的 $product 变量总是返回一行(如果 product_id 是主键或唯一列,那么就是这种情况)您可以使用 ->first() 而不是 ->get() 因为 ->first() 只返回一个对象,其中 ->get() 返回一个集合。

如果你使用 ->first() 你应该得到这样的结果集

{#222 ▼
+"product_id": 48
+"product_name": "xxxxxxx"
+"product_number": 400
+"product_price": 300
+"product_describe": "qqqqqqqqqq"
+"product_status_id": 1
+"product_category_id": 1
+"product_buy_price": 200
+"created_at": null
+"updated_at": null
}

然后你可以将 $product 对象转换为这样的数组 $product_array = (array)$product; 你会得到如下所示的数组

array[
"product_id"=> 48
"product_name"=> "xxxxxxx"
"product_number"=> 400
"product_price"=> 300
"product_describe"=> "qqqqqqqqqq"
"product_status_id"=> 1
"product_category_id"=> 1
"product_buy_price"=> 200
"created_at"=> null
"updated_at"=> null
]

然后你可以添加任何你想要的元素

$buy_number = array('buynumber' => $buynumber);
$merge = array_merge($product_array, $buy_number);

或者你可以简单地做

$product_array['buynumber'] = $buynumber;

这会让你得到这样的结果

array[
"product_id"=> 48
"product_name"=> "xxxxxxx"
"product_number"=> 400
"product_price"=> 300
"product_describe"=> "qqqqqqqqqq"
"product_status_id"=> 1
"product_category_id"=> 1
"product_buy_price"=> 200
"created_at"=> null
"updated_at"=> null
"buynumber"=>"whatever the buy number you passed here"
]

如果您的 $product 变量返回多个结果,那么您不能使用 ->first()。然后您可以遍历 $product_array 并将每个元素转换为数组,然后添加必要的元素

关于php - 拉维尔 : How do I add key and value into Collection or array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59403019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com