PHP错误。为什么“变量未定义”?在array_map里面? [英] PHP error. Why is "variable undefined" inside array_map?

查看:225
本文介绍了PHP错误。为什么“变量未定义”?在array_map里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using array_map function in my php application. I defined the array_map function like this.

$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

When i print the $ratingID . i prints the value correctly , so $ratingID is defined. Why it is undfined in array_map function? My $rated_item_array is

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错误。为什么“变量未定义”?在array_map里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆