missing number

Discussion in 'PHP' started by mero2020, Dec 13, 2013.

  1. #1
    I have a array like this
    (1-1,1-2,1-4,2-2,2-4)

    And I want to retrieve the missing

    For example, I want to retrieve from the previous array (1-3,2-1,2-3)
     
    mero2020, Dec 13, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    That's gonna be hard to do. You cannot "retrieve" something which isn't there. And, you need to specify max-amounts (ie, maximum 1-4), or else any function won't know how to stop. But, if you state a maximum, you could probably iterate through the array via a couple loops, and just check to see if the value is already in the array.
    Something like this: (pseudocode, only based on the information provided):
    
    <?php
    $start_array = array('1-1','1-2','1-4','2-2','2-4');
    for ($i = 1; $i <= 2; $i++) { //this is based on the max count of the first number of each array, in this case two
      for ($c = 1; $c <= 4; $c++) {
      if (!in_array($i.'-'.$c,$start_array)) {
      echo $i.'-'.$c.', ';
      }
      }
    }
    ?>
    
    PHP:
     
    PoPSiCLe, Dec 13, 2013 IP
  3. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    I can't state a maximum, it's folders and each folder has numbers of papers,and i want to have missing
     
    mero2020, Dec 13, 2013 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Questions like this where I can't understand the logic behind doing something like this usually means you are using the wrong logic when writing your code. Perhaps rethink what you are trying to do and rewrite your code to make it easier.
     
    NetStar, Dec 13, 2013 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    If it's folders, why didn't you say so to begin with? Then read everything from the folder into an array, iterate through that array, and then find the missing items. But again, you will need to have a maximum number SOMEHOW, or else the iteration won't know when to stop counting.
     
    PoPSiCLe, Dec 13, 2013 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    Assuming the maximum number is the max of what's in the directory (and that x-y, y <= 9):

    
    // Get list of files
    $hasFiles = array_diff(scandir('folder/'), array('..', '.'));
    
    // If your files have extension, uncomment
    //array_walk($hasFiles, create_function('&$val', '$temp = explode(".", $val); $val = $temp[0];'));
    
    // Sort them naturally
    natsort($hasFiles);
    
    // Get max left/right from current array
    list($xMax, $yMax) = explode('-', array_pop($hasFiles));
    $listFiles = array();
    
    // Loop through them (assume 9 is the max)
    for ($x = 1; $x <= $xMax; $x++){
       $y = 1;
       while ($y <= 9 && ($x != $xMax || $y != $yMax)) {
         $listFiles[] = $x . '-' . $y++;
       }
    }
    
    // Get missing
    $missing = array_diff($listFiles, $hasFiles);
    
    // Print them
    print_r($missing);
    
    PHP:
     
    Last edited: Dec 14, 2013
    ThePHPMaster, Dec 14, 2013 IP