Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts

    array_map vs array_walk

    As the title suggests, I'm a bit confused on array_walk vs array_map. I'm reading up on them, reading examples, and I'm not really grasping what the difference between them are. I mean, even array_filter seems the same, and I can't figure out when/where I should be using each.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is a good question. On a basic level, array_map and array_walk can be used almost interchangably.
    Array_map works on multiple arrays, working on each value of an array at a time. You cannot work with keys on the highest level array. Each corresponding offset in each array is compared against the other:
    PHP Code:
    function cb($a$b)
    {
        return 
    $a $b;
    }

    $a1 = array(135);
    $a2 = array(246);

    $ar array_map('cb'$a1$a2);
    print_r($ar); 
    The results are:
    Code:
    Array
    (
        [0] => 3
        [1] => 7
        [2] => 11
    )
    In the event that an offset is unset, it treats it as null instead.

    Array_walk only works on one array, and is used to manipulate it from either a key or value perspective. You must specify the value as a reference if you want it to change, it will ignore any type of return.

    Array_filter is used to separate your array into different parts. It does this by a boolean comparator, so you only return true or false. The example on the API is great actually, the odd/even number splitting makes great sense. Array_filter cannot be used to modify your array.

    Does that help you out?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Keleth (09-23-2010)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    So, if I'm following correctly, array_map is most useful difference is getting multiple arrays to interact? e.g. like your example, adding elements of arrays? Verse array_walk being a function applied to each individual member of an array? In which case, making array_walk the better choice for when feeding an individual array?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That about sums it up yeah.
    You can see why at the most basic level array_map and array_walk are almost identical. Both can manipulate the values of an array, but only array_walk can manipulate the keys of the array. If you were to do some sorting, you'd want to work with array_walk for example, since you can then alter the keys of the given ones (though I wouldn't sort using array_walk anyway, as thats just a waste of a simple loop >.<).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, I've got a multi-dimensional array I wanna run trim on (trim just being an example in this case), so I was thinking of running a recursive function to basically trim all variables inside, so I was thinking of using array_walk. But from what you say, as I know how deep the array goes, loops might be better. At first, I was thinking it'd be less efficient, but really, that's probably what the functions are doing internally anyway

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Don't forget that array_walk also has a array_walk_recursive. It won't return the key for any element which is an array (regardless of if its explicit or not), but I'll bet you'll find that very handy to use.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, but like you mentioned, in this case, looping might be better, as internally it has to loop anyway


  •  

    Posting Permissions

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