Home >Backend Development >PHP Tutorial > 一个数组值空缺的计算,该如何解决

一个数组值空缺的计算,该如何解决

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 13:29:42962browse

一个数组值空缺的计算
假如数组值为
$a=array('1','8','2','3','11');
要计算出11以下那些4,5,6,7,9,10还没有存在于该数组怎么计算?

因为这个数组的最大值11可能是任意数,所以要根据最大值来确定比他小的值还有那些没有在该数组中。。。。

------解决方案--------------------

PHP code
$a=array('1','8','2','3','11');
$b = range($a[0], $a[count($a) - 1]);

$result = array_diff($b, $a); //缺少的值,即$b与$a的差集
print_r($result); <div class="clear">
                 
              
              
        
            </div>
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