db->insert_id(); $rated_i-6ren">
gpt4 book ai didi

PHP 错误。为什么 "variable undefined"在 array_map 里面?

转载 作者:IT王子 更新时间:2023-10-29 00:59:53 26 4
gpt4 key购买 nike

我在我的 php 应用程序中使用 array_map 函数。我这样定义了 array_map 函数。

$ratingID =  $this->db->insert_id();

$rated_item_array = array_map(function ($a) {
return $a + array('RatingID' => $ratingID);
}, $rated_item_array);

PHP通知来了

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: ratingID

当我打印 $ratingID 时。我正确地打印了值,因此定义了 $ratingID。为什么它在array_map 函数中是未定义的? 我的 $rated_item_array

Array
(
[0] => Array
(
[RatingFactorPreferenceID] => 1,
[PreferenceID] => 45,
[RatedValue] => 1,
[CreatedOn] => 1326790338,
[CreatedBy] => 25
)

[1] => Array
(
[RatingFactorPreferenceID] => 2,
[PreferenceID] => 45,
[RatedValue] => 1,
[CreatedOn] => 1326790338,
[CreatedBy] => 25
)

[2] => Array
(
[RatingFactorPreferenceID] => 3,
[PreferenceID] => 45,
[RatedValue] => 1,
[CreatedOn] => 1326790338,
[CreatedBy] => 25
)
)

最佳答案

$rated_item_array = array_map(
function ($a) use ($ratingID){
return $a + array('RatingID' => $ratingID );
},
$rated_item_array
);

关于PHP 错误。为什么 "variable undefined"在 array_map 里面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891888/

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