search
HomeBackend DevelopmentPHP ProblemHow to perform union sum in php array

How to perform union sum in php array

Oct 17, 2022 pm 07:21 PM
phpphp array

Implementation steps: 1. Use array_merge() to merge multiple arrays into one array, the syntax is "array_merge(array 1, array 2...)"; 2. Use array_unique() to remove the elements in the merged array For repeated values, the syntax "array_unique (merge array)" will return a union array; 3. Use the array_sum() function to calculate the sum of the union arrays, the syntax "array_sum (union array)".

How to perform union sum in php array

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

Array union is to combine two or more The collection after array merging and deduplication.

In PHP arrays, you can use the array_merge() function, array_unique() function and array_sum() function to perform union summation.

Implementation idea:

  • First merge the arrays and find the union of two or more arrays

  • Then sum the unions

Implementation steps:

Step 1 : Use the array_merge() function to merge arrays

array_merge() function will merge the elements of multiple arrays, and the elements of the next array will be appended to the previous array, and finally return the merged array.

Note: If the original array has the same string key name, then the value after the key name will overwrite the previous value; if the original array contains a numeric key name, then the later value will The previous value is not overwritten, but appended to it.

Example: Merge 3 arrays

<?php
header("Content-type:text/html;charset=utf-8");
$arr1 = [1,2,3,4,5];
$arr2 = [2,3,6,7,8,9];
$arr3 = [10];
var_dump($arr1);
var_dump($arr2);
var_dump($arr3);
$result = array_merge($arr1,$arr2,$arr3);
echo "合并数组后:";
var_dump($result);
?>

How to perform union sum in php array

Step 2: Use the array_unique() function to remove duplicate values ​​from the merged array to obtain the array Union

array_unique() function can remove duplicate values ​​from the array

$bj=array_unique($result);
echo "并集数组:";
var_dump($bj);

How to perform union sum in php array

Step 3: Use array_sum() function to calculate the union The sum of arrays

array_sum() function can calculate the sum of all elements in an array.

$sum=array_sum($bj);
echo "并集之和:".$sum;

How to perform union sum in php array

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to perform union sum in php array. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor