Table of Contents
array_map
array_reduce
Summary
Home Backend Development PHP Tutorial Quick methods to change the array structure: array_map, array_reduce, etc.

Quick methods to change the array structure: array_map, array_reduce, etc.

Jun 20, 2023 pm 04:51 PM
array_map array_reduce array structure

Arrays are frequently used data structures in programming, and when processing arrays, changing their structures is a common requirement. In the PHP language, there are many built-in functions that can be used to accomplish this purpose, such as array_map, array_reduce, etc. This article will introduce and practical applications of these functions.

array_map

array_map function is a variable function in PHP (variable function means that you can use variables as function names in the code). This function is used to convert all the elements in an array Elements are converted by the specified callback function and a new array is returned. The number and order of elements in the new array are consistent with the original array.

Syntax: array_map(callback,array1,array2...)

  • callback: required, specifies the callback function. In the callback function, a variable name must be specified for each parameter, and a value should be returned with return.
  • array1: required, specifies the array.
  • array2...: Optional, specifies the array.

Example 1:

$a = [1,2,3,4,5];

function square($n)
{
    return $n * $n;
}

$b = array_map("square", $a);

print_r($b);
Copy after login

Output result: Array ( [0] => 1 [1] => 4 [2] => 9 [3 ] => 16 [4] => 25 )

Example 2:

$a1 = [1,2,3];
$a2 = ['one', 'two', 'three'];

function combine($n1, $n2)
{
    return $n1 . $n2;
}

$b = array_map("combine", $a1, $a2);

print_r($b);
Copy after login

Output result: Array ( [0] => 1one [1] => 2two [2] => 3three )

array_reduce

The array_reduce function is used to iterate all elements in the array one by one by specifying a callback function and return a single value.

Syntax: array_reduce (array, callback, [initial_value])

  • array: required, specifies the array.
  • callback: required, specified function. The function accepts two parameters: the result of the previous iteration and the current element.
  • initial_value: Optional, specifies the first value used for logical processing.

Example one:

$a = [1, 2, 3, 4, 5];

$sum = array_reduce($a, function($total, $num){
    return $total + $num;
});

echo $sum;
Copy after login

Output result: 15

Example two:

$a = ['Hello', 'World', '!'];

$sentence = array_reduce($a, function($sentence, $word){
    return $sentence . ' ' . $word;
});

echo $sentence;
Copy after login

Output result: Hello World!

Summary

array_map and array_reduce functions are very practical array traversal functions. They can help us quickly change the structure of the array to meet our needs. Everyone can apply it flexibly in development and make practical applications based on their own needs.

The above is the detailed content of Quick methods to change the array structure: array_map, array_reduce, etc.. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles