Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP error. Why is "variable undefined" inside 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 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         ) ) 
like image 230
Kanishka Panamaldeniya Avatar asked Jan 17 '12 08:01

Kanishka Panamaldeniya


People also ask

Why is my variable undefined PHP?

Undefined variable: the variable's definition is not found in the project files, configured include paths, or among the PHP predefined variables. Variable might have not been defined: there are one or more paths to reach the line with the variable usage without defining it.

What is Array_map function in PHP?

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function. Tip: You can assign one array to the function, or as many as you like.

Is there a map function in PHP?

The Ds\Map::map() function of the Map class in PHP is used to apply a callback function to a Map object. This returns the result of applying the callback function to each value present on the map.


1 Answers

$rated_item_array = array_map(   function ($a) use ($ratingID){      return $a + array('RatingID' => $ratingID );    },    $rated_item_array ); 
like image 141
xdazz Avatar answered Oct 01 '22 08:10

xdazz