Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 974029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:22:11+00:00 2026-05-16T03:22:11+00:00

What exactly is the difference between array_map , array_walk and array_filter . What I

  • 0

What exactly is the difference between array_map, array_walk and array_filter. What I could see from documentation is that you could pass a callback function to perform an action on the supplied array. But I don’t seem to find any particular difference between them.

Do they perform the same thing?
Can they be used interchangeably?

I would appreciate your help with illustrative example if they are different at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:22:12+00:00Added an answer on May 16, 2026 at 3:22 am
    • Changing Values:
      • array_map cannot change the values inside input array(s) while array_walk can; in particular, array_map never changes its arguments.
    • Array Keys Access:
    • array_map cannot operate with the array keys, array_walk can.
    • Return Value:
    • array_map returns a new array, array_walk only returns true. Hence, if you don’t want to create an array as a result of traversing one array, you should use array_walk.
    • Iterating Multiple Arrays:
    • array_map also can receive an arbitrary number of arrays and it can iterate over them in parallel, while array_walk operates only on one.
    • Passing Arbitrary Data to Callback:
    • array_walk can receive an extra arbitrary parameter to pass to the callback. This mostly irrelevant since PHP 5.3 (when anonymous functions were introduced).
    • Length of Returned Array:
    • The resulting array of array_map has the same length as that of the largest input array; array_walk does not return an array but at the same time it cannot alter the number of elements of original array; array_filter picks only a subset of the elements of the array according to a filtering function. It does preserve the keys.

    Example:

    <pre>
    <?php
    
    $origarray1 = array(2.4, 2.6, 3.5);
    $origarray2 = array(2.4, 2.6, 3.5);
    
    print_r(array_map('floor', $origarray1)); // $origarray1 stays the same
    
    // changes $origarray2
    array_walk($origarray2, function (&$v, $k) { $v = floor($v); }); 
    print_r($origarray2);
    
    // this is a more proper use of array_walk
    array_walk($origarray1, function ($v, $k) { echo "$k => $v", "\n"; });
    
    // array_map accepts several arrays
    print_r(
        array_map(function ($a, $b) { return $a * $b; }, $origarray1, $origarray2)
    );
    
    // select only elements that are > 2.5
    print_r(
        array_filter($origarray1, function ($a) { return $a > 2.5; })
    );
    
    ?>
    </pre>
    

    Result:

    Array
    (
        [0] => 2
        [1] => 2
        [2] => 3
    )
    Array
    (
        [0] => 2
        [1] => 2
        [2] => 3
    )
    0 => 2.4
    1 => 2.6
    2 => 3.5
    Array
    (
        [0] => 4.8
        [1] => 5.2
        [2] => 10.5
    )
    Array
    (
        [1] => 2.6
        [2] => 3.5
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What exactly does SignedCookieJar do? And what is the difference from that and using
What exactly is the difference in rails between dev and prod environments. When I
What exactly is the difference between shuffle and array_rand functions in PHP? Which is
In this post , @kbrimington mentions that there is a subtle difference between which
What exactly is the difference between: scala> def foo = 5 foo: Int and
What exactly is the difference between the HintPath in a .csproj file and the
1.) What's the difference between these two queries, exactly? $( #orderedlist li ) $(
What is exactly the difference between registers in the Modbus protocol data model? The
I am trying to figure out what exactly is the difference between debug configuration
What exactly is the difference between pcap_loop and pcap_dispatch?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.