jaikar Posted July 11, 2009 Share Posted July 11, 2009 hi there, i am trying to use a method in array_walk.. something like this class test { function one() { $t = array('one', 'two'); array_walk($t, '$this0>converttolower'); } function convertolower($value, $key) { $t[$key] = strtolower($value); } } the above is not working, how to make something like this work. thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/ Share on other sites More sharing options...
papaface Posted July 11, 2009 Share Posted July 11, 2009 array_walk($t, '$this0>converttolower'); should be array_walk($t, $this->converttolower); I think Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-873543 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 array_walk($t, array(&$this, 'converttolower')); Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-873578 Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 or just cut the middleman out and use array_map class test { function one() { $t = array('one', 'two'); $t = array_map('strtolower',$t); } } Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-873665 Share on other sites More sharing options...
jaikar Posted July 12, 2009 Author Share Posted July 12, 2009 Awesome Crayon !.. it worked like charm for my purpose... ignace, your idea did not worked.. i dont know why. it said invalid argument. but still, to my question... there is no way to use a method as argument in array_walk ? thankyou! Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874079 Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 It is possible, but you have to pass it as the static method version not with the $this shortcut, because it won't parse the $this. Also, you had a couple other things wrong. First, your method is called converttolower but you were trying to array_walk convertolower (missing a t in there). 2nd, you weren't doing it right in your converttolower method. In order to change it, you have to pass the value by reference, and then assign the alteration to the passed value. You cannot array_walk and make changes to the original array the way you were doing ($t[blah]) as that will cause array_walk to go AWOL. So here's how it would look: class test { function one() { $t = array('ONE', 'tWo'); array_walk($t, 'test::converttolower'); } function converttolower(&$value, $key) { $value = strtolower($value); } } Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874083 Share on other sites More sharing options...
Daniel0 Posted July 12, 2009 Share Posted July 12, 2009 OP, see: http://php.net/manual/en/language.pseudo-types.php#language.types.callback array_walk($t, array(&$this, 'converttolower')); Objects are passed by reference by default. Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874115 Share on other sites More sharing options...
jaikar Posted July 12, 2009 Author Share Posted July 12, 2009 Crayon, ... Awesome !.. and a fantastic explanation .. .. respect !!.... thankyou very much !!. additinally, you also explained the value by referance concept, which would be next question for i did not know why & was used !... actualy, i studied this long back but forgot and never used this in anycodes. . thankyou !.. Dan, !.. happy to see you again !! ... thankyou for the doc link .. it really expains a lot, i did not got this link in google, but after seing the link, i noticed how to pull up help for arguments and callbacks.. php doc is awesome as always!! i have one more clarification, i have a situation where i have to compare values of 2 arrays.. like $arr1 = array('one', 'two'); $arr2 = array('one', 'three', 'four'); comparing like, if $arr2 have atlest one value in arr1, then some code will execute. question is .. is it possble to do this without using a loop ? is there a funciton for this in php ? thankyou ! Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874159 Share on other sites More sharing options...
Daniel0 Posted July 12, 2009 Share Posted July 12, 2009 You could do like: if (count(array_insersect($arr1, $arr2))) { // they share at least one element } else { // they don't } Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874162 Share on other sites More sharing options...
jaikar Posted July 12, 2009 Author Share Posted July 12, 2009 Dan, YOU ARE THE MANNNNNNNNNNNN !!.. i almost thought there is no function for this task... just tested and works perfect !!! ... i am speechless now ... never thought array_intersect is used for this.. it just says "Computes the intersection of arrays". i was not able to understand that. i am really surpriced for your knowledge in php !! ... thankyou so much Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874173 Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 well that's kind of what intersection means...an intersection is where something meets. A more common use of that word is with roads and streets. When they cross, that's an intersection. Quote Link to comment https://forums.phpfreaks.com/topic/165610-solved-how-to-use-a-method-in-array_walk/#findComment-874177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.