PHP error. Why is “variable undefined” inside array_map?

不问归期 提交于 2019-12-20 09:48:30

问题


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 notice comes

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: ratingID

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
        )
)

回答1:


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


来源:https://stackoverflow.com/questions/8891888/php-error-why-is-variable-undefined-inside-array-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!