search
HomeDaily ProgrammingPHP KnowledgePHP calculates the elements in an array whose sum of three values ​​equals 0


PHP calculates the elements whose three values ​​add up to 0 in an array, which may be a little difficult for some PHP beginners. In fact, it is very simple as long as you understand the calculation idea. Here we need to understand the array_push function in PHP.

PHP calculates the elements in an array whose sum of three values ​​equals 0

Recommended reference study: "PHP Tutorial"

Below we will directly introduce PHP calculations to you with specific code examples. Method for adding three elements in an array to equal 0.

The code example is as follows:

<?php

//PHP计算数组中三个值相加等于0的元素
function aaa($arr){
    $count = count($arr) - 2;
    $result=[];
    for ($x = 0; $x < $count; $x++){
        if($arr[$x]+ $arr[$x+1] + $arr[$x+2] == 0){
            array_push($result,"{$arr[$x]} + {$arr[$x + 1]}+{$arr[$x + 2]} =0");
        }
    }
    return $result;
}

 $arr = array(-1,0,1,2,-1,-4);

print_r(aaa($arr));

Here, the elements whose three values ​​in the array $arr add up to 0 are calculated. The result is as follows:

PHP calculates the elements in an array whose sum of three values ​​equals 0

Introduction to related functions:

count function —Calculate the number of units in an array, or the number of attributes in an object.

array_push function—Push one or more cells to the end of the array (push)

array_push ( array &$array , mixed $value1 [, mixed $... ] ) : int

array_push() treats array as a stack and passes in The variable is pushed to the end of the array. The length of array will increase according to the number of variables pushed onto the stack. Has the same effect as:

<?php
$array[] = $var;
?>

and repeats the above action for each incoming value.

Note: If you use array_push() to add a unit to the array, it is better to use $array[] =, because there is no additional burden of calling the function.

array_push() will issue a warning if the first argument is not an array. This is different from the behavior of $var[], which creates a new array.

The parameter array represents the input array. value1 represents the first value to be pushed at the end of array.

The return value is the number of elements in the array after return processing.

This article is about PHP's method of calculating the elements whose three values ​​add up to 0 in an array. It's actually very simple. I hope it will be helpful to friends who need it!


The above is the detailed content of PHP calculates the elements in an array whose sum of three values ​​equals 0. 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 Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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.