Home > Backend Development > PHP Tutorial > 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

高洛峰
Release: 2023-03-04 13:12:01
Original
1596 people have browsed it

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 );
 ?>
Copy after login

Online operation

Output result:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template