S
S
Sergey Kiryanov2019-08-01 15:31:41
PHP
Sergey Kiryanov, 2019-08-01 15:31:41

How to find the missing elements of an array?

There is an array like:

array(3000) {
  [0]=>
  int(3000)
  [1]=>
  int(2999)
  [2]=>
  int(2998)
  [3]=>
  int(2997)
  .....
}


That is, the output is a list of numbers in order from largest to smallest, with a step of one
How to find out the missing numbers (there may be several in a row) and write them to a separate array?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Anton B, 2019-08-01
@Dizzy221

$array = [...]; // ваш исходный массив

$array_full = range(min($array), max($array)); // эталонный массив

$array_diff = array_diff($array_full, $array); // пропущенные значения

S
Siverius, 2019-08-01
@Siverius

$myArray = [];
for($i = 0; $i < 3000; $i++){
    if(!isset($array[$i])) {
        $myArray[] = $i;
    }
}

0
0xD34F, 2019-08-01
@0xD34F

$missed = array_diff(range($arr[0], $arr[count($arr) - 1], -1), $arr);

K
Kerm, 2019-08-01
@Kerm

foreach ($array as $key => $value) {
    $value = trim($value);
    if (empty($value))
        echo "$key empty <br/>";
    else
        echo "$key not empty <br/>";
}

A
Alexey Chertok, 2017-11-30
@BarnyBroken

Read about translate3d in CSS. Very easy to do.
Example: theory.phphtml.net/css/translate3d.html

P
Pavel Kornilov, 2017-11-30
@KorniloFF

On pure JS, without CSS3 - something like this
. Accordingly, you can change the function of the motion trajectory to the required one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question