Home  >  Article  >  Backend Development  >  php array_map uses a custom function to process each value in the array

php array_map uses a custom function to process each value in the array

高洛峰
高洛峰Original
2016-12-30 11:02:271524browse

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

Description

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

array_map() function converts user-defined functions Applies to each value in the array and returns the array with the 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.

Parameter introduction:

php array_map使用自定义的函数处理数组中的每个值

Return value

Returns an array, each element of the array is in the array (arr1) Each element is processed through a callback function.

Example:

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

Online operation

Output result:

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

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more php array_map uses custom functions to process each value in the array, please pay attention to 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