Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 31st, 2008, 3:31 PM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Function too convoluted?

That's pretty intense...

I only assumed it does. I thought create_function returns a reference to the anonymous function, just as a function name is a reference to a named function.

PHP Syntax (Toggle Plain Text)
  1. $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
  2. echo $newfunc(2, M_E);

Is equivilent to:
PHP Syntax (Toggle Plain Text)
  1. function newfunc ($a,$b) { return "ln($a) + ln($b) = " . log($a * $b); }
  2. echo $newfunc(2, M_E);
Sane is offline   Reply With Quote
Old Jan 31st, 2008, 3:56 PM   #12
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Function too convoluted?

Doesn't appear to be possible as I get the following error messages when I tested that little snippet out:
Undefined variable: newfunc in ... on line #
Fatal error: Function name must be a string in ... on line #
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Feb 1st, 2008, 10:33 AM   #13
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Function too convoluted?

Figured it out Sane, rather than doing it that way you would use call_user_func. So that array_filter_multi would become
PHP Syntax (Toggle Plain Text)
  1. function array_filter_multi($array, $callback, $filtered_output = ""){
  2. $ret = array();
  3. foreach($array as $key => $value){
  4. if([i]call_user_func($callback, $key, $value)[/i]){
  5. if(is_array($value)){
  6. $ret[$key] = array_filter_multi($value, $callback, $filtered_output);
  7. }elseif(is_object($value)){
  8. $ret[$key] = array_filter_multi(get_object_vars($value), $callback, $filtered_output);
  9. }else{
  10. $ret[$key] = $value;
  11. }
  12. }else{
  13. $ret[$key] = $filtered_output;
  14. }
  15. }
  16. return $ret;
  17. }
Then you could just write a function such as
PHP Syntax (Toggle Plain Text)
  1. function comparator($key, $value){
  2. return $key < $value;
  3. }
And finally pass it to the previous function by doing the following
PHP Syntax (Toggle Plain Text)
  1. array_filter_multi($data, "comparator", null);
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Feb 1st, 2008, 10:39 AM   #14
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Function too convoluted?

I will have to add that to my arsenal of handy functions.
Sane is offline   Reply With Quote
Old Feb 1st, 2008, 10:40 AM   #15
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Function too convoluted?

One final correction it can work the way you said Sane, but it needs one extra step:
PHP Syntax (Toggle Plain Text)
  1. function comparator($key, $value){
  2. return $key < $value;
  3. }
  4. $foo = 'comparator';
  5. print_r($foo(0, 1));
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Feb 1st, 2008, 10:47 AM   #16
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Function too convoluted?

Ah, I see. So it would have worked the way I said, except when I posted:

function callback_func($key, $value) { return ($value < 2 || $value > 70); }

print_r(array_filter_multi($data, $callback_func, null));

It was actually:

function callback_func($key, $value) { return ($value < 2 || $value > 70); }

print_r(array_filter_multi($data, 'callback_func', null));
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
'function prototype'.... Konnor C++ 3 Sep 4th, 2005 6:19 AM
Recommended Practice for returning data from function Arla C# 1 Aug 16th, 2005 12:21 PM
change the empty function from the old format to the new format powah Sed and Awk 0 Jun 23rd, 2005 11:10 AM
Sort function vs. sort_heap function + Transform Function Josef_Stalin C++ 2 May 2nd, 2005 11:18 AM
Returning a value from a variable to the main function colt C 3 Apr 28th, 2005 7:56 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC