itaka itaka - 1 year ago 94
PHP Question

array_map in php with callback OOP

Possible Duplicate:

How to use an object method as a callback function

Usually I used array_map with procedural code, but in this case I'm working in OOP and the callback should be "this->id2areas", but It's not working. Is there anyway of put this callback with OOP?

ERROR MESSAGE: array_map() expects parameter 1 to be a valid callback, function 'this->id2area' not found or invalid function name


$this->context->assign('user_areas', implode(', ', array_map('id2area', explode(',', $this->user['areas']))));
explode(',', $this->user['areas']))));

function id2area($id) {//callback
if ($id == 0) {
return 'National';
$query = "SELECT area FROM area WHERE id = $id";
return DB::fetch_instance()->slave->fetchColumn($query);

Answer Source

In PHP, you can use an array to associate an object and a method call as a callable

array_map(array($this, 'id2area'), $array);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download