Home  >  Article  >  Backend Development  >  Array_map() function details in php

Array_map() function details in php

PHP中文网
PHP中文网Original
2017-11-01 09:58:021260browse

array_map applies the callback function to the cells of the given array

array array_map ( callable $callback , array $arr1 [, array $... ] )

array_map() function applies the user-defined function to each element in the array value, and returns an array with new values ​​after the user-defined function is applied.

The number of parameters accepted by the callback function should be consistent with the number of arrays passed to the array_map() function.

callback Required. A callback function that operates on each element of each array. ​

arr1 Required. Array of callbacks to be executed.

array Optional. A list of arrays that will be executed by the callback function.

Return value

Returns an array. Each element of the array is processed by the callback function (callback) for each element in the array (arr1).

Example:

<?php
 function cube ( $n )
{
  return( $n * $n * $n );
}
 
 $a = array( 1 , 2 , 3 , 4 , 5 );
 $b = array_map ( "cube" , $a );
 print_r ( $b );
 ?>

Output result:

<?php
    Array
    (
      [0] => 1
      [1] => 8
      [2] => 27
      [3] => 64
      [4] => 125
    )
?>

Apply the function to each value in the array, multiply each value by itself, and return Array with new values:

<?php
function myfunction($v)
{
return($v*$v);
}
$a=array(1,2,3,4,5);
print_r(array_map("myfunction",$a));
?>

Definition and usage

array_map() function applies a user-defined function to each value in the array and returns the user-defined function Define an array with new values ​​after the function is applied.

Tip: You can input one or more arrays to the function.

array_map(myfunction,array1,array2,array3...)

myfunction Required. The name of the user-defined function, or null.

array1 Required. Specifies an array.

array2 Optional. Specifies an array.

array3 Optional. Specifies an array.

Returns an array containing the value of array1 after the user-defined function is applied.

Use a user-defined function to change the value of the array:

<?php
    function myfunction($v)
    {
    if ($v==="Dog")
    {
    return "Fido";
    }
    return $v;
    }
    $a=array("Horse","Dog","Cat");
    print_r(array_map("myfunction",$a));
?>

Use two arrays:

<?php
    function myfunction($v1,$v2)
    {
    if ($v1===$v2)
    {
    return "same";
    }
    return "different";
    }
    $a1=array("Horse","Dog","Cat");
    $a2=array("Cow","Dog","Rat");
    print_r(array_map("myfunction",$a1,$a2));
?>

Change all letters of the values ​​in the array to uppercase:

<?php
    function myfunction($v) 
    {
    $v=strtoupper($v);
    return $v;
    }
    $a=array("Animal" => "horse", "Type" => "mammal");
    print_r(array_map("myfunction",$a));
?>

When assigning the function name to null:

<?php
$a1=array("Dog","Cat");
$a2=array("Puppy","Kitten");
print_r(array_map(null,$a1,$a2));
?>


The above is the detailed content of Array_map() function details in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn