SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)

    Is there an array_map() for methods?

    Hi, I've got this code which does work.

    It runs HTMLPurifier on the last values in a multi-array which is written to an ini fiile using PEAR::Config_Lite.

    I spoofed a Purifier class in order to explain my question.

    Is there anything similar to array_map that I could run on a class method instead of this nested foreach loop, which I really don't like looking at.

    PHP Code:
    // incoming
    $test['en']['this'] = 'that';
    $test['en']['the'] = 'other';
    $test['fr']['this'] = 'ceci';
    $test['fr']['the'] = 'cela';

    class 
    Purifier{  // spoofing the real class
      
    function purify($x){
      return 
    $x '!'
      }
    }

    $purifier = new Purifier ;
       
    $config = array();

        foreach(
    $test as $lang=>$array){
            
    $r = array();
            foreach(
    $array as $k=>$v){  // buerk!
            
    $r[$k]  = $purifier->purify($v);
            }
            
    $config[$lang] = $r;
        }

    die(
    var_export($config)); 
    Or is there really no other way?

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,054
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I think if it were a static method, yes, you could. But with it being instantiated, that may prove to be much harder. Of course, before I posted, this I did a quick search and proved myself wrong

    PHP Code:
    array_map(array($instance'method_name'), $ar
    Last edited by cpradio; Mar 21, 2013 at 08:41. Reason: Added url of search result
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $config = array();
        foreach(
    $test as $l=>$array)
          
    $config[$l]  = array_map(array($purifier'purify'), $array); 
    Duh, search for your question title before posting ... who'd have thought it ...

    Nice one thanks both.

    EDIT, gaaargh .. now I spot it mentioned in the comments of the manual page.

    Still, on the plus side I did at least spot that it could be improved and had a hunch what the solution ought to be -- so I must be improving, I just need to clean my glasses.

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Have you tried array_walk_recursive?

    PHP Code:
    <?php
    $test
    ['en']['this'] = 'that';
    $test['en']['the'] = 'other';
    $test['fr']['this'] = 'ceci';
    $test['fr']['the'] = 'cela';

    array_walk_recursive($test, function(&$item, &$key) use ($purifier) {
        if (!
    is_array($item)) $item $purifier->purify($item); 
    });

    print_r($test);

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by TomB View Post
    PHP Code:
    if (!is_array($item)) $item $purifier->purify($item); 
    Just a quick note to say that is_array() will never be true. array_walk_recursive() does not send arrays to the callback function.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Off Topic:


    Quote Originally Posted by Salathe View Post
    Off Topic:



    Just a quick note to say that is_array() will never be true. array_walk_recursive() does not send arrays to the callback function.

    Aha, thanks I did wonder about that when I posted it and having read the manual again it does make it clear, although I didn't spot it at the time! A poor case of RTFM.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •