How to find an ordinary sequence of Keys in an Array?

Asked

Viewed 82 times

-1

The goal is to find the next ordinary sequence of 03 Keys within a array with broken indexes, in the example would be 4,5,6 or 7,8,9

CODE

<?php
        $myArray = array(
        'item_1',
        'item_2',
        'item_3',
        'item_4',
        'item_5',
        'item_6',
        'item_7',
        'item_8',
        'item_9',
        'item_10',
        'item_11',
    );

    $arrOriginal = $myArray;

    unset($myArray[2]);
    unset($myArray[3]);

    print_r(PHP_EOL . "Lista índices Quebrados" . PHP_EOL);
    foreach ($myArray as $keyItem => $itemValue) {
        echo 'Key: ' . $keyItem . ', Valor: ' . $itemValue . PHP_EOL;
    }

    print_r(PHP_EOL . "Lista Original" . PHP_EOL);

    foreach ($arrOriginal as $keyItem => $itemValue) {

        echo 'Key: ' . $keyItem . ', Valor: ' . $itemValue . PHP_EOL;
    }

RETURN

Lista índices Quebrados

Key: 0, Valor: item_1
Key: 1, Valor: item_2
Key: 4, Valor: item_5
Key: 5, Valor: item_6
Key: 6, Valor: item_7
Key: 7, Valor: item_8
Key: 8, Valor: item_9
Key: 9, Valor: item_10
Key: 10, Valor: item_11

Lista Original

Key: 0, Valor: item_1
Key: 1, Valor: item_2
Key: 2, Valor: item_3
Key: 3, Valor: item_4
Key: 4, Valor: item_5
Key: 5, Valor: item_6
Key: 6, Valor: item_7
Key: 7, Valor: item_8
Key: 8, Valor: item_9
Key: 9, Valor: item_10
Key: 10, Valor: item_11

EXPECTED RESULT IN THIS EXAMPLE

4,5,6 or 7,8,9

  • For example if instead of unset($myArray[2]); unset($myArray[3]); that unset($myArray[3]); unset($myArray[7]); the result would be [0,1,2], [4,5,6] and , [8,9,10]?

  • Actually unset is the action of removing an item in the code, but the idea is to take only consecutive sequences of 3 indices within an array in which there will be missing indices as in the example.

  • That I want to know if I remove those specific indices, 3 and the 7, the result will be [0,1,2], [4,5,6] and [8,9,10] or it will be something different as only the first occurrence [0,1,2]?

  • That, [0,1,2], [4,5,6] and [8,9,10]

  • 1

    I am implementing a reserve list, in which I am generating the hours every 30 minutes, assuming that the reserve lasts 90 consecutive minutes I imagine it will take 03 indices within the array. So when the user is going to book, only the reservation that starts in the Index that contains +2 forward has to appear.

  • Got it, at the moment I’m closing the office(wetland time zone). When you get home I give you a strength.

Show 1 more comment

1 answer

1


<?php
        $myArray = array(
        'item_1',
        'item_2',
        'item_3',
        'item_4',
        'item_5',
        'item_6',
        'item_7',
        'item_8',
        'item_9',
        'item_10',
        'item_11'
    );


    unset($myArray[2]);
    unset($myArray[3]);

    // Cria um array com as chaves de $myArray
    $chaves = array_keys($myArray); 

    // Inicializa a array que receberá as arrays de chaves consecutivas 
    $consecutivos = [];

    // Pega a quantidade de chaves
    $tamanho = count($chaves);

    // Verifica se há mais de duas chaves
    if($tamanho > 2){
       // Caso haja mais de duas chaves itera por elas menos as duas últimas
       for ($i=0; $i < ($tamanho - 2) ; $i++){
          // Verifica se as 3 chaves, a partir de índice $i, são consecutivas
          if($chaves[$i] === ($chaves[$i+1] - 1) && 
             $chaves[$i] === ($chaves[$i+2] - 2)   ) {
               //Caso sejam consecutivas as coloca no array $consecutivos[]
               $consecutivos[] = [$chaves[$i], $chaves[$i+1] , $chaves[$i+2]];
               //Despreza o dois próximos índices a partir de índice $i
               $i += 2;
          } 
       }  
    }

    print_r( $consecutivos);

Returning:

Array
(
    [0] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [1] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

Link to the code: https://repl.it/repls/SquareFruitfulExponents

  • Perfect, note 10. Thank you Augusto Vasques.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.