Jump to content

[SOLVED] how to use a method in array_walk


jaikar

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);
   }
}

Link to comment
Share on other sites

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 !

 

 

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.