PHP - array_map used for array looping?

1 replies
PHP: array_map - Manual


I used this to process a directory of files with certain extensions. Looking at examples it seems this may also be used to parse arrays to a callback function.

I don't fully understand this function.

Could this be used instead of a for or foreach loop?

Is it faster or slower? anyone know?

Just seemed pretty handy for certain situations but beyond what I am using it for in one specific instance, I doubt I will ever use it.

What are benefits of this function? I guess I was wondering if it is viable option as replacement for other loops, I understand it is only for arrays.


Thanks,

- T
#array #arraymap #looping #php
  • Profile picture of the author ussher
    its quicker to write than a foreach loop.

    Good for stuff like trimming the whitespace off each of the elements in the array so you dont end up with 'something' ' something' and 'something ' thinking they are 3 different things.

    That usage is like this:
    $_POST = array_map('trim', $_POST);
    php global trim $_post - Stack Overflow

    could use it for anything though

    function print_hello($name){
    print 'hello '.$name;
    }
    $everyone = array('peter', 'paul', 'john', 'ringo');
    array_map('print_hello', $everyone);
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[5118054].message }}

Trending Topics