InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP

    PHP strings

    • PHP - split string examples
    • PHP - take last n characters of a string
    • PHP check if string contains something

    PHP Array

    • PHP - associative array value in double quoted string
    • PHP - check if a value is in array
    • PHP - convert array to associative array
    • PHP - print array in one line
    • PHP array foreach - code snippets
    • PHP array map example
    • PHP check if key exists in array
    • PHP sort associative array using custom compare function
    • php get array value with default

    PHP Regex

    • PHP Regex

    PHP Cookbook

    • PHP - assign multi lines string to a variable
    • PHP - call a function with arguments in array examples
    • PHP - get calling file name and line number using debug_backtrace
    • PHP - get class name and file name from an object
    • PHP - get function arguments examples
    • PHP - how to catch errors using set_error_handler
    • PHP - how to initialize static variables
    • PHP - how to use function static variable as cache
    • PHP check if a filename is valid
    • PHP elapsed time
    • PHP execute command and capture shell exit status
    • PHP how to get current url
    • PHP print number to two decimal places
    • PHP remove trailing whitespaces and newline
    • Php equal (==) vs identical (===)
    • Running php eval on code with tags

    PHP functions online

    • PHP functions online
     
    • Home
    • > Tutorials
    • > PHP

    PHP array map example

    on Feb 9, 2016

    PHP array map applies the callback function to one or more arrays. In callback we can use a function or anonymous function. Here are some examples.

    array_map with one array

    Here we’ll use anonymous function which adds 1.

    <?php
    $a = array(10, 20, 30, 40);
    $new = array_map(function($v) {return $v+1;}, $a);
    print_r($new);
    ?>
    Array
    (
        [0] => 11
        [1] => 21
        [2] => 31
        [3] => 41
    )
    
    Env: PHP 7.3.18 (Linux)

    array_map with multiple arrays

    Here we’ll use function which add two arguments. Also note that in case arrays have different lengths, null is passed for missing values.

    <?php
    $a = array(10, 20, 30, 40);
    $b = array(1, 2, 3);
    function mysum($v1, $v2) {
      return $v1+$v2;
    }
    $new = array_map("mysum", $a, $b);
    print_r($new);
    ?>
    Array
    (
        [0] => 11
        [1] => 22
        [2] => 33
        [3] => 40
    )
    
    Env: PHP 7.3.18 (Linux)

    Suggested posts:

    1. Bash associative array tutorial
    2. PHP – how to initialize static variables
    3. PHP – convert array to associative array
    4. PHP – print array in one line
    5. PHP array foreach – code snippets
    6. PHP – check if a value is in array
    7. PHP check if key exists in array
    8. PHP sort associative array using custom compare function
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged PHP, PHP arrays, PHP cookbook, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2022 InfoHeap.

    Powered by WordPress