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)
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:
I can't state a maximum, it's folders and each folder has numbers of papers,and i want to have missing
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.
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.
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: