Jump to content

array_map() for non-array variable


redcore

Recommended Posts

Hello everyone! I have a unique problem. Basically my ultimate goal is to be able to apply functions in a similar way array_map() does but without having to be in an array. Confusing right?

 

So instead of calling a function like...

do_something($var);

 

I'd like to do this...

unknown_function($var, 'do_something');

 

Does so-called "unknown_function" exist? Or can you just not call a function in this way for non-arrays?

 

Sorry if this is confusing! :) Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/206059-array_map-for-non-array-variable/
Share on other sites

Its easy enough to make.

 

function do_something($var) {
  echo "This is $var within do_something()";
}

function call($arg, $callback) {
  if (function_exists($callback)) {
    $callback($arg);
  }
}

call('Hello', 'do_something');

 

I'm kinda missing the point though. There are likely better ways to approach this if you tell us what your actually trying to do.

Yeah, it's sort of odd, I guess.

 

I pass a string of field names ("tableA, tableB, tableC") to a query builder that returns the result in an array (automated). The downside is that is there's no opportunity to modify the resulting data, like converting MySQL datetime to PHP time() or something. My goal is to pass functions with the field names to modify any particular field I want. If you know a better way than this, that'd be great to hear :) Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.