search
HomeDaily ProgrammingPHP KnowledgePHP check missing numbers in array



PHP checks for possible missing numbers in the array, which can also be understood as calculating the difference between the elements in the two arrays. So here we can achieve it through the two functions range and array_diff in PHP.

PHP check missing numbers in array

Recommended reference study: "PHP Tutorial"

Below we will introduce PHP check arrays with specific code examples. Methods for numbers that may be missing.

The code example is as follows:

<?php
//PHP检查数组中可能缺少的数字
function aaa($num_list){
    
    //创建一个数组
    $new_arr = range($num_list[0],max($num_list));
    
    //使用array_diff查找缺少的元素
    return array_diff($new_arr,$num_list);
}
echo "<pre class="brush:php;toolbar:false">";
print_r(aaa(array(1,2,3,6,7,8)));

print_r(aaa(array(10,11,12,14,15,16,17)));

Here we create an aaa method to calculate the missing numbers in the two arrays in the above code.

The missing elements in the output are as follows:

PHP check missing numbers in array

As shown in the figure, the first array is missing the numbers 4 and 5; the second array is missing Number 13.

Introduction to important functions:

range function means creating an array based on the range, containing the specified elements

Syntax:

range ( mixed $start , mixed $end [, number $step = 1 ] ) : array

Create an array containing cells in the specified range.

Parameter: start represents the first value of the sequence. end represents the value at which the sequence ends. step means that if the step size step is set, it will be used as the step value between units. step should be positive. If step is not set, it defaults to 1.

The return value is the cells from start to end (including start and end) in the returned array.

array_diff functionRepresents the calculation of the difference set of arrays

Syntax:

array_diff ( array $array1 , array $array2 [, array $... ] ) : array

Compares array1 with one or more other arrays, and returns the value in array1 but Values ​​not in other arrays.

Parameters: array1 represents the array to be compared; array2 represents comparison with this array; ... represents more compared arrays.

The return value means returning an array that includes all values ​​in array1 but not in any other parameter array. Note that the key names remain unchanged.

This article is an introduction to the method of checking possible missing numbers in an array in PHP. It is also one of the common PHP interview questions. It is very simple. I hope it will be helpful to friends in need!



The above is the detailed content of PHP check missing numbers in 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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development 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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)