class something{
public function add_val( $val ){
$array = array();
foreach( $val as $value ) {
$array[] = static::$post[${$value}];
}
return $array;
}
pulblic function somethingelse(){
....
....
$optionsArray['value'] = array_map( 'add_val', array_chunk( $drop_val, count( $optionsArray['heading_x'] ) ) );
....
....
}
}
how can i call the add_val method within the other using array_map()??
Use an array that contains the object, and the method name:
You do the same for most other functions that take in callbacks as parameters, like
array_walk(),call_user_func(),call_user_func_array(), and so on.How does it work? Well, if you pass an array to the callback parameter, PHP does something similar to this (for
array_map()):Here you can see that PHP just loops through your array, calling the method on each value. Nothing complicated to it; again just basic object-oriented code.
This may or may not be how PHP does it internally, but conceptually it’s the same.