Search code examples
phparray-map

Issue with array_map callback


At first I had this (work in wamp but not in my web server)

$ids = array_map(function($item) { return $item['user_id']; }, $data['student_teacher']);`

So I try to convert the code to that but nothing work ( i got Array,Array,Array,Array,Array,Array from outpout )

$ids = array_map($this->myarraymap(null), $data['student_teacher']);

function myarraymap($item) {
        return $item['user_id']; 

    }

Solution

  • You need to pass it a callback, and not actually pass it the execution of the function, i.e.,

    $ids = array_map(array($this, 'myarraymap'), $data['student_teacher']);
    
    function myarraymap($item) {
       return $item['user_id']; 
    }