Does php have map?

藏色散人
Release: 2023-02-26 17:14:01
Original
10502 people have browsed it

Does php have map?

Does php have a map?

php has a map, that is, the array_map() function applies user-defined functions to on each value in the array, and returns the array with the new value 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.

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

Syntax

array_map(myfunction,array1,array2,array3...)
Copy after login

Parameters

myfunction is 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.

Return Value: Returns an array containing the values ​​of array1, after applying the custom function to each value.

Example

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

Output:

Array ( [0] => Horse [1] => Fido [2] => Cat )
Copy after login

More PHP related For knowledge, please visit PHP中文网!

The above is the detailed content of Does php have map?. For more information, please follow other related articles on 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!