[PHP] array_walk, or array_map, or foreach?
PHP List, I took a snippet of code right off the php.net site to use trim on all the elements of an array. Theoretically, it should test if the element in an array is in turn another array, and break it down to the next level until it gets to a string it can use trim on. This is the code: public static function trimArray($array) { if (is_array($array)) { array_walk($array, trimArray); } else { $array = trim($array); } return $array; } The function exists inside a static class called Utility where I keep all basic utility functions. I don't know if it's the fact that it's in a static class that makes a difference, but I've tried the following variations on the line with array_walk() in it: array_walk($array, Utlity::trimArray) array_map(Utility::trimArray, $array) array_map(trimArray, $array) I've even tried accomplishing it with a foreach(), but no matter what I do, it doesn't work. As it walks through the array, it seems to trim a copy of the element in the array, trim that, but leave the original array untouched. What am I missing here? -- Dave M G Ubuntu 6.06 LTS Kernel 2.6.17.7 Pentium D Dual Core Processor PHP 5, MySQL 5, Apache 2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_walk, or array_map, or foreach?
On Fri, 2006-10-20 at 16:04 +0900, Dave M G wrote: PHP List, I took a snippet of code right off the php.net site to use trim on all the elements of an array. Theoretically, it should test if the element in an array is in turn another array, and break it down to the next level until it gets to a string it can use trim on. This is the code: public static function trimArray($array) { if (is_array($array)) { array_walk($array, trimArray); I'm too lazy too look, but usually when using a class method as a handler for PHP callback functions you pass the method as follows: array_walk( $array, array( 'ClassName', 'trimArray' ) ); Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array_Walk error messages
Can anyone give me a clue what I have done wrong here. //These would be passed as ints: array_walk($l_industry, 'mysql_escape_string'); Warning: Wrong parameter count for mysql_escape_string() in /xxx//public_html/Test2.php on line 104 //These would be passed as ints: array_walk($l_tterm, 'mysql_escape_string'); Warning: Wrong parameter count for mysql_escape_string() in /xxx//public_html/Test2.php on line 105 //These are strings array_walk($skills, 'mysql_escape_string'); Warning: Wrong parameter count for mysql_escape_string() in /xxx/x/public_html/Test2.php on line 106 //These are ints: array_walk($skys, 'mysql_real_escape_string'); Warning: mysql_real_escape_string() expects parameter 2 to be resource, integer given in /xxx/x/public_html/Test2.php on line 107 //These are ints: array_walk($slus, 'mysql_escape_string'); Warning: Wrong parameter count for mysql_escape_string() in /xxx//public_html/Test2.php on line 108 Thanks Stuart -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array_Walk error messages
* Thus wrote Stuart Felenstein: Can anyone give me a clue what I have done wrong here. //These would be passed as ints: array_walk($l_industry, 'mysql_escape_string'); Warning: Wrong parameter count for mysql_escape_string() in /xxx//public_html/Test2.php on line 104 The callback function form array_walk expects to parameters: callback($key, $value); But mysql_escape_string() only takes one. As suggested earlier, use array_map() instead. Curt -- Quoth the Raven, Nevermore. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_walk
If you use array_walk with an associative array, the only arguments that PHP automatically passes to the defined array are the numerical/element keys. Is there any way you can force it to pass the associative keys? For example, I have an array: $bob = array( this = other ); Instead of passing this and other to your array, PHP passes 0 and other. Is there any way I can make it so that it passes this? Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_walk
Instead of passing this and other to your array, PHP passes 0 and other. Is there any way I can make it so that it passes this? [pulled from the manual directly] Example 1. array_walk() example $fruits = array (d=lemon, a=orange, b=banana, c=apple); function test_alter ($item1, $key, $prefix) { $item1 = $prefix: $item1; } function test_print ($item2, $key) { echo $key. $item2br\n; } echo Before ...:\n; array_walk ($fruits, 'test_print'); reset ($fruits); array_walk ($fruits, 'test_alter', 'fruit'); echo ... and after:\n; reset ($fruits); array_walk ($fruits, 'test_print'); The printout of the program above will be: Before ...: d. lemon a. orange b. banana c. apple and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple -- Dan Hardiker [[EMAIL PROTECTED]] ADAM Software Systems Engineer First Creative Ltd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_walk inside class method
Dear Gyozo You have sent me the following solution thru php mailing list. I have also asked you a doubt about the syntax. I want to explain this syntax to my colleagues, could u pls point me to any documentation of this syntax or any article. if possible pls explain yourself if u have time. It is urgent . pls Thanks in Advance On Mon, 7 Jan 2002, S. Murali Krishna wrote: Hi Thanks for ur help. Its Working but I couldn't understand the syntax given by u can u explain pls. Thanks again. On Sun, 6 Jan 2002, Gyozo Papp wrote: it may be better: array_walk($this-array, array($this, 'func1'); | | | Hi All, | I want to use array_walk function inside a class method. But the | problem is i want the second argument to array_walk ( function name ) | be a another function of the same class. when i gave like that its telling | | Error : function not exist. | | class some() | { | | function func1() | { | } | function func2() | { | array_walk($array,func1); | } | } | | What is the syntax or way to do this. | | | Thanks in Advance. | | S.Murali Krishna | [EMAIL PROTECTED] | = | We grow slow trying to be great | | - E. Stanley Jones | - | | | -- | PHP General Mailing List (http://www.php.net/) | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | To contact the list administrators, e-mail: [EMAIL PROTECTED] | | S.Murali Krishna [EMAIL PROTECTED] = We grow slow trying to be great - E. Stanley Jones - S.Murali Krishna [EMAIL PROTECTED] = We grow slow trying to be great - E. Stanley Jones - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] array_walk inside class method
Hi Thanks for ur help. Its Working but I couldn't understand the syntax given by u can u explain pls. Thanks again. On Sun, 6 Jan 2002, Gyozo Papp wrote: it may be better: array_walk($this-array, array($this, 'func1'); Attila Strauss [EMAIL PROTECTED] wrote in message 001d01c195db$67647da0$[EMAIL PROTECTED]">news:001d01c195db$67647da0$[EMAIL PROTECTED]... | hi | | $this-func1(); | | | best regards | attila | | | | | Hi All, | I want to use array_walk function inside a class method. But the | problem is i want the second argument to array_walk ( function name ) | be a another function of the same class. when i gave like that its telling | | Error : function not exist. | | class some() | { | | function func1() | { | } | function func2() | { | array_walk($array,func1); | } | } | | What is the syntax or way to do this. | | | Thanks in Advance. | | S.Murali Krishna | [EMAIL PROTECTED] | = | We grow slow trying to be great | | - E. Stanley Jones | - | | | -- | PHP General Mailing List (http://www.php.net/) | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | To contact the list administrators, e-mail: [EMAIL PROTECTED] | | S.Murali Krishna [EMAIL PROTECTED] = We grow slow trying to be great - E. Stanley Jones - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] array_walk inside class method
Hi All, I want to use array_walk function inside a class method. But the problem is i want the second argument to array_walk ( function name ) be a another function of the same class. when i gave like that its telling Error : function not exist. class some() { function func1() { } function func2() { array_walk($array,func1); } } What is the syntax or way to do this. Thanks in Advance. S.Murali Krishna [EMAIL PROTECTED] = We grow slow trying to be great - E. Stanley Jones - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] array_walk inside class method
hi $this-func1(); best regards attila Hi All, I want to use array_walk function inside a class method. But the problem is i want the second argument to array_walk ( function name ) be a another function of the same class. when i gave like that its telling Error : function not exist. class some() { function func1() { } function func2() { array_walk($array,func1); } } What is the syntax or way to do this. Thanks in Advance. S.Murali Krishna [EMAIL PROTECTED] = We grow slow trying to be great - E. Stanley Jones - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] array_walk inside class method
it may be better: array_walk($this-array, array($this, 'func1'); Attila Strauss [EMAIL PROTECTED] wrote in message 001d01c195db$67647da0$[EMAIL PROTECTED]">news:001d01c195db$67647da0$[EMAIL PROTECTED]... | hi | | $this-func1(); | | | best regards | attila | | | | | Hi All, | I want to use array_walk function inside a class method. But the | problem is i want the second argument to array_walk ( function name ) | be a another function of the same class. when i gave like that its telling | | Error : function not exist. | | class some() | { | | function func1() | { | } | function func2() | { | array_walk($array,func1); | } | } | | What is the syntax or way to do this. | | | Thanks in Advance. | | S.Murali Krishna | [EMAIL PROTECTED] | = | We grow slow trying to be great | | - E. Stanley Jones | - | | | -- | PHP General Mailing List (http://www.php.net/) | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | To contact the list administrators, e-mail: [EMAIL PROTECTED] | |
Re: [PHP] array_walk() + class methods
Much obliged, that did the trick. On Fri, 21 Dec 2001 05:48:31 -0500, Papp Gyozo wrote: array_walk($ar, array($object, 'methodname')); - Original Message - From: Jeff Levy [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, December 13, 2001 1:17 PM Subject: [PHP] array_walk() + class methods | I'd like to call a class method with array_walk(). Is this possible? | | class widget { | function sicballs($arg){ | print $arg; | } | } | | $x = new widget(); | | $ar = array('my','values'); | | array_walk($ar, ???) | | ... | | the only way I've been able to get this (simplified) code to work the way | I want it is: | | function snafu($arg) { | global $x; | call_user_method(sicballs, $x, $arg); | } | | obviously, I want to call the method direcly, without writing this | function, snafu()... | | anyone know? | | -- | PHP General Mailing List (http://www.php.net/) | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | To contact the list administrators, e-mail: [EMAIL PROTECTED] | -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] array_walk() + class methods
array_walk($ar, array($object, 'methodname')); - Original Message - From: Jeff Levy [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, December 13, 2001 1:17 PM Subject: [PHP] array_walk() + class methods | I'd like to call a class method with array_walk(). Is this possible? | | class widget { | function sicballs($arg){ | print $arg; | } | } | | $x = new widget(); | | $ar = array('my','values'); | | array_walk($ar, ???) | | ... | | the only way I've been able to get this (simplified) code to work the way | I want it is: | | function snafu($arg) { | global $x; | call_user_method(sicballs, $x, $arg); | } | | obviously, I want to call the method direcly, without writing this | function, snafu()... | | anyone know? | | -- | PHP General Mailing List (http://www.php.net/) | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | To contact the list administrators, e-mail: [EMAIL PROTECTED] |
[PHP] array_walk() and class methods
i have class instance snagfu, of class snafu, with a method, fubar() i have an array, $tarfu, and want to run it through array_walk(), array_walk($tarfu, $snagfu-fubar) seems to be logical, if it's possible. this obviously doesn't work. How do i use a class method with array walk? or is it even possible? if it's not possible, why not? seems to be as if it should be possible... thanks. Jeff Levy Software Design Meta-Craft Creations -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] array_walk() + class methods
I'd like to call a class method with array_walk(). Is this possible? class widget { function sicballs($arg){ print $arg; } } $x = new widget(); $ar = array('my','values'); array_walk($ar, ???) ... the only way I've been able to get this (simplified) code to work the way I want it is: function snafu($arg) { global $x; call_user_method(sicballs, $x, $arg); } obviously, I want to call the method direcly, without writing this function, snafu()... anyone know? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] array_walk in obejct
hi, whats about using the array_walk function within an object? php says that there is no callback function. do you have an idea? thx, marcbey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]