I'm trying to create an algorithm that returns a price depending on number of hours. But the distance between the number of hours are varying. For example I have an array:
$set = [
1 => 0.5,
2 => 1,
3 => 1.5,
4 => 2,
5 => 2.5,
12 => 4
];
$value = 3;
end($set);
$limit = (int)key($set);
foreach($set as $v => $k) {
// WRONG, doesn't account for varying distance
if($value >= $v && $value <= $v) {
if($value <= $limit) {
return $k;
} else {
return false;
}
}
}
The trouble is, the distance between 5 and 12 register as null. I might as well use $value == $v instead as the line I've marked as incorrect does anyway.
So I was wondering if there was a better way to round up to the next index in that array and return the value for it?
Cheers in advance!
Try this:
$set = array(1 => 0.5, 2 => 1, 3 => 1.5, 4 => 2, 5 => 2.5, 12 => 4);
function whatever(idx, ary){
if(in_array(idx, array_keys(ary))){
return ary[idx];
}
else{
foreach(ary as $i => $v){
if($i > idx){
return $v;
}
}
}
return false;
}
echo whatever(7, $set);
The problem is that $v is a single value, so $value >= $v && $value <= $v is equivalent to $value == $v.
Instead, consider that if the loop hasn't ended, then the cutoff hasn't been reached yet - and a current "best price" is recorded. This requires that the keys are iterated in a well-ordered manner that can be stepped, but the logic can be updated for a descending order as well.
$price_chart = [
1 => 0.5,
2 => 1,
3 => 1.5,
4 => 2,
5 => 2.5,
12 => 4
];
function get_price ($hours) {
global $price_chart;
$best_price = 0;
foreach($price_chart as $min_hours => $price) {
if($hours >= $min_hours) {
// continue to next higher bracket, but remember the best price
// which is issued for this time bracket
$best_price = $price;
continue;
} else {
// "before" the next time cut-off, $hours < $min_hours
return $best_price;
}
}
// $hours > all $min_hours
return $best_price;
}
See the ideone demo. This code could also be updated to "fill in" the $price_chart, such that a price could be found simply by $price_chart[$hours] - but such is left as an exercise.
Related
I am working in a scoring system in php. When user chooses specific question, get extra points.
If anyone has a working PHP demo with code of a scoring system, he would be welcome. I mostly need a working "for" function:
"for question 1, if answer is abc, 5 points, else 0 points. for question 2, if aswer is bcd, 10 points, if answer is cft, 10 points, else 0 points", etc.
I would not want to have to fill my code with endless if statements.
For my current attempt, The error it displays:
Fatal error: Uncaught Error: Call to undefined function value() in C:\xampp\htdocs\score.php:18 Stack trace: #0 {main} thrown in C:\xampp\htdocs\score.php on line 18
Edit: I realized i might not have defined the "value" variable. It should give the value of "1 point" for now. How should i define it?
<?php
// Define questions and the correct answers
$questions = array(
'AB01' => 3, // correct answer to question AB01 is 3
'AB02' => 1, // correct answer to AB02 is code 1
'AB03' => 4, // correct answer to AB03 is hidden behind code 4
'AB04' => 2,
'AB05' => 1
// and so on
);
// Initialize counter variable
$points = 0;
// Check all questions in a loop
foreach ($questions as $variable=>$correct) {
// Call up participant's answer
$answer = value($variable);
// Check and count point if applicable
if ($answer == $correct) {
$points++; // synonymous with $points = $points + 1
}
}
// Show result ...
html('<p>You have scored'.$points.' points.</p>');
// ... or store in an internal variable
put('IV01_01', $points);
// ...
// Check all questions in a loop
foreach ($questions as $variable=>$correct) {
if (value($variable) == $correct) {
$points++;
}
}
// Show result or otherwise process
// ...
// Define questions and the values of possible answers
$questions = array(
'AB01' => array(1 => 2, 2 => 5, 3 => 3), // In question AB01, answer 1 has value 2, 2 has value 5, 3 has value 3
'AB02' => array(1 => 5, 2 => 4, 3 => 1), // In question AB02, values 5 (answer 1), 4 (2) und 1 (3) are assigned
'AB03' => array(1 => 0, 2 => 0, 3 => 5),
'AB04' => array(1 => 4, 2 => 0, 3 => 3),
'AB05' => array(1 => 2, 2 => 2, 3 => 5),
// u.s.w.
);
// Initialize counter variable
$points = 0;
// By using foreach you can just pass through the key-value pairs
foreach ($questions as $variable => $values) {
// Call up participant's answer
$answer = value($variable);
// Check if there is a value available for this answer (otherwise, do not award a point)
if (isset($values[$answer])) {
// Count the value
$points += $values[$answer];
}
}
// Show result or otherwise process
html('<p>You have scored'.$points.' points.</p>');
$points = valueSum('AB01');
// Show result or otherwise process
html('<p>You have scored '.$points.' points.</p>');
// Call up list of all items
$items = getItems('AB01');
// Initialize points variable
$points = 0;
// Pass through all items
foreach ($items as $item) {
// Question ID still has to be assembled
$id = 'AB01_'.sprintf('%02d', $item);
// Call up participant's answer
$answer = value($id);
// An error code may have been saved (e.g. -1 for "no response")
// Otherwise, count the answer
if ($answer > 0) {
$points += $answer;
}
}
// Show result or otherwise process
html('<p>You have scored '.$points.' points.</p>');
$points += $answer - 1;
// List of items - providing polarity in each case
$items = array(
'01' => +1,
'02' => -1,
'03' => +1,
'04' => +1,
'05' => -1
// and so on.
);
// Initialization of points variable
$points = 0;
// Pass through all items
foreach ($items as $item => $polarity) {
// Here the variable ID is built from the question and item IDs
$id = 'AB01_'.$item;
// Call up respondent's answer
$answer = value($id);
// Ignore if it is not a valid response
if ($answer < 1) {
// This means the rest are ignored in the FOR/FOREACH loop
continue;
}
// "Invert" answers
if ($polarity < 0) {
// In a 5-point scale, the inverted answer code has a value of 6
// the constant has to be adjusted for other differentations.
$answer = 6 - $answer;
}
// Add up
$points += $answer;
}
// Show result or otherwise process.
html('<p>You have scored'.$points.' points.</p>');
if (
(value('AB01_01') == 2) and
(value('AB01_02') == 2) and
(value('AB01_03') == 1) and
(value('AB01_04') == 1)
) {
// Count point, jump to a different part, or just display a message
html('<p>Correct</p>');
} else {
html('<p>Incorrect</p>');
}
// Define questions and their correct answers
// Only items are defined that will also be checked
$questions = array(
// In question AB01, 1 and 2 have to be checked, 3 and 4 must not be checked
'AB01' => array(1 => 2, 2 => 2, 3 => 1, 4 => 1),
// In question AB02, 2 and 3 have to be checked, 4 must not, and the value for 1 is irrelevant
'AB02' => array( 2 => 2, 3 => 2, 4 => 1),
// In AB03, all 4 have to be checked
'AB03' => array(1 => 2, 2 => 2, 3 => 2, 4 => 2),
// and so on.
'AB04' => array(1 => 1, 2 => 2, 3 => 1, 4 => 2),
'AB05' => array(1 => 2, 2 => 1, 3 => 2 )
);
// Initialize counter variable
$points = 0;
// Pass through all questions
foreach ($questions as $questionId => $answers) {
// Set the error counter for this question to 0
$error = 0;
foreach ($answers as $itemId => $default) {
// Assemble item ID
$id = $questionId.'_'.$itemId;
// Call up participant's answer
$answer = value($id);
// Verify answer is correct (actually: for falsehood)
if ($answer != $default) {
// In the event of any deviation, count as an error
$error++;
}
}
// Check if the question has been correctly answered
if ($error == 0) {
// Award a point
$points++;
}
}
// Show result or otherwise process
html('<p>You have scored '.$points.' points.</p>');
if ($points < 10) {
text('feedback1');
} elseif ($points < 20) {
text('feedback2');
} else {
text('feedback3');
}
$type = valueMean('AB01');
$use = valueMean('AB02');
if ($type < 1.5) {
text('typeA');
} elseif ($type <= 4.5) {
text('typeB');
} else {
text('typeC');
}
if ($use < 2.0) {
text('useA');
} elseif ($use < 4.0) {
text('useB');
} else {
text('useC');
}
?>
You will need to define a "value" function.
<?php
//Define it anywhere within the <?php tag
function value($arg){
// Your code logic here
}
I'm writing a PHP script for available rooms in a hotel. I want every combination for a group (i.e. 4 person). This is my array.
$room_array = array(
array(
"title" => "1 person room",
"room_for" => 1,
"price" => 79
),
array(
"title" => "2 person room with other",
"room_for" => 1,
"price" => 69
),
array(
"title" => "2 person room alone",
"room_for" => 1,
"price" => 89
),
array(
"title" => "2 person",
"room_for" => 2,
"price" => 69
),
array(
"title" => "3 person",
"room_for" => 3,
"price" => 69
)
);
Possible outcome:
4x 1 person room
4x 2 person room with other
3x 1 person room + 1x 2 person room with other
2x 2 person room
1x 3 person room + 1x 1 person room
etc. etc.
This calls for a recursive function. But every example I looked at doesn't work with counting in the inner array. The closest i found was this question:
Finding potential combinations of numbers for a sum (given a number set to select from)
But i didn't get de solution to work..
UPDATE:
Hi, thanks for all the answers. Really helped me in finding the best practice. In the meantime, the assignment has changed a little so I can't answer my own original question. My problem is solved. Thanks again for the help!
My answer below will get you partway there.
Resources
I borrowed some code logic from this answer.
To quote the answer (in case of future removal), please view below.
You can try
echo "<pre>";
$sum = 12 ; //SUM
$array = array(6,1,3,11,2,5,12);
$list = array();
# Extract All Unique Conbinations
extractList($array, $list);
#Filter By SUM = $sum $list =
array_filter($list,function($var) use ($sum) { return(array_sum($var) == $sum);});
#Return Output
var_dump($list);
Output
array
0 => array
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
4 => string '6' (length=1)
1 => array
1 => string '1' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
2 => array
1 => string '1' (length=1)
2 => string '11' (length=2)
3 => array
1 => string '12' (length=2)
Functions Used
function extractList($array, &$list, $temp = array()) {
if(count($temp) > 0 && ! in_array($temp, $list))
$list[] = $temp;
for($i = 0; $i < sizeof($array); $i ++) {
$copy = $array;
$elem = array_splice($copy, $i, 1);
if (sizeof($copy) > 0) {
$add = array_merge($temp, array($elem[0]));
sort($add);
extractList($copy, $list, $add);
} else {
$add = array_merge($temp, array($elem[0]));
sort($add);
if (! in_array($temp, $list)) {
$list[] = $add;
}
}
}
}
My answer
The code below uses the code referenced above. I changed the return functionality of the array_filter function to map it to your needs.
The only thing left for you to do is change the function so that it can catch multiple of the same type of room. At the moment, the code below will only output 1 of each type of room (as per the code referenced above). An easy way to get around this would be to multiply the array values you send to the function by the number of guests you are searching for rooms, but up to the amount of rooms available. So: if you are looking to book for 4 guests and you have no single rooms remaining and only 1 double room, your best match result would have to be a 2 person room and a 3 person room. I've added some brief functionality to add this (it's commented out), although I have not tested it. It will likely take a while to process that as well so if you're looking for a quicker method, you're gonna have to use a better algorithm as already mentioned in previous comments/answers or solve P vs NP
The code below also gives you the option to toggle a value of $exact. This value, if set to true, will return only matches exactly equal to the number of guests, and if set to false will return all matches that equal to at least the number of guests.
<?php
class Booking {
private $minGuests = 1;
protected $guests = 1;
protected $rooms = [];
public function getRoomCombinations(bool $exact = true) {
$guests = $this->guests;
$list = [];
$rooms = $this->rooms;
/*for($i = 0; $i < $guests-1; $i++) {
$rooms = array_merge($rooms, $this->rooms);
}
asort($rooms);*/
$this->extractList($rooms, $list);
$result = array_filter($list, function($var) use ($guests, $exact) {
if($exact)
return(array_sum(array_map(function($item) { return $item['room_for'];}, $var)) == $guests);
else
return(array_sum(array_map(function($item) { return $item['room_for'];}, $var)) >= $guests && count($var) <= $guests);
});
array_multisort(array_map('count', $result), SORT_ASC, $result);
return $result;
}
private function extractList(array $array, array &$list, array $temp = []) {
if (count($temp) > 0 && !in_array($temp, $list))
$list[] = $temp;
for($i = 0; $i < sizeof($array); $i++) {
$copy = $array;
$elem = array_splice($copy, $i, 1);
if (sizeof($copy) > 0) {
$add = array_merge($temp, array($elem[0]));
sort($add);
$this->extractList($copy, $list, $add);
} else {
$add = array_merge($temp, array($elem[0]));
sort($add);
if (!in_array($temp, $list)) {
$list[] = $add;
}
}
}
}
public function setGuests(int $guests) {
$this->guests = ($guests >= $this->minGuests ? $guests : $this->minGuests);
return $this;
}
public function setHotelRooms(array $rooms) {
$this->rooms = $rooms;
return $this;
}
}
$booking = (new Booking())
->setGuests(4)
->setHotelRooms([
[
"title" => "1 person room",
"room_for" => 1,
"price" => 79
],
[
"title" => "2 person room with other",
"room_for" => 1,
"price" => 69
],
[
"title" => "2 person room alone",
"room_for" => 1,
"price" => 89
],
[
"title" => "2 person",
"room_for" => 2,
"price" => 69
],
[
"title" => "3 person",
"room_for" => 3,
"price" => 69
]
]);
echo '<pre>' . var_export($booking->getRoomCombinations(true), true) . '</pre>';
?>
If you need all the combinations then you can use an backtracking iterative algorithm (depth path).
In summary:
Type of tree: binary tree because all the levels can contain a solution when the number of persons contabilized = objetive
Binary tree
Algorithm functions
You need to increment the cont every time that a level is generated with the number of persons of the level and decrement when you change your track (exploring brothers or back)
solution: array[0..levels-1] values {0 (node not selected) ,1 (node selected)}
solution[0] = 1 -> You choose that "1 person room" belongs to the solution
solutions: list/array of objects and every object contains array of titles of rooms
function Backtracking ()
level:= 1
solution:= s_initial
end:= false
repeat
generate(level, solution)
IF solution(level, solution) then
save_solution
else if test(level, solution) then
level:= level+ 1
else
while NOT MoreBrothers(level, solution)
go_back(level, s)
until level==0
2.1. Generate: generate next node
2.2. Solution: test if it's a solution
2.3. Critery: if we must continue by this track or bound
2.4. MoreBrothers: if there are nodes without check at this level
2.5. Backtrack: all the nodes at this level were explored
2.6. Save solution: add to the solutions array your object that contains strings
$room_array = array(
array(
"title" => "1 person room",
"room_for" => 1,
"price" => 79
),
array(
"title" => "2 person room with other",
"room_for" => 1,
"price" => 69
),
array(
"title" => "2 person room alone",
"room_for" => 1,
"price" => 89
),
array(
"title" => "2 person",
"room_for" => 2,
"price" => 69
),
array(
"title" => "3 person",
"room_for" => 3,
"price" => 69
)
);
// Gets rooms based on a given number of guests
function get_possible_rooms($num_guests) {
global $room_array;
$possible_rooms = [];
foreach ($room_array as $room) {
if ($num_guests <= $room['room_for']) {
$possible_rooms[] = $room['title'];
}
}
return $possible_rooms;
}
// Gets the available room capacities
function get_room_capacities() {
global $room_array;
$capacities = [];
foreach ($room_array as $room) {
$capacities[] = $room['room_for'];
}
return array_unique($capacities);
}
// Gets the different combinations of groups of guests based on the room capacities
function get_guest_assignments($remaining_guests, $parent_id = '', $num_guests, &$result) {
$room_capacities = get_room_capacities();
for ($i = 1; $i <= $remaining_guests; ++$i) {
if (in_array($i, $room_capacities)) {
$parent_guests = (isset($result[$parent_id])) ? $result[$parent_id] : 0;
$result[$parent_id . $i] = $parent_guests + $i;
for ($j = 1; $j <= $remaining_guests - $i; ++$j) {
// Recursively get the results for children
get_guest_assignments($j, $parent_id . $i, $num_guests, $result);
}
}
}
if ($remaining_guests === 1 && $parent_id !== '') {
// If it reaches the end and it does not fulfill the required number of guests,
// mark it for removal later
if ($result[$parent_id] < $num_guests) {
$result[$parent_id] = null;
}
}
// This is the last recursion
if ($result[$parent_id . '1'] === $num_guests) {
// Remove duplicates.
// To do this, we need to re-sort the keys (e.g. 21 becomes 12) and call array_unique()
// I admit this is a bit sloppy implementation.
$combinations = [];
foreach ($result as $key => $value) {
if ($value !== null) {
$nums = str_split($key);
sort($nums);
$combinations[] = implode('', $nums);
}
}
$result = array_unique($combinations);
}
}
// Gets the rooms for each group of guest
function get_room_assignments($guest_str) {
$rooms = [];
for ($i = 0; $i < strlen($guest_str); ++$i) {
$num_guests = intval(substr($guest_str, $i, 1));
$rooms[] = get_possible_rooms($num_guests);
}
return $rooms;
}
//----------
// RUN
//----------
$guests = 4;
$result = [];
get_guest_assignments($guests, null, $guests, $result);
foreach ($result as $guest_combi) {
$assignments = get_room_assignments($guest_combi);
// Printing output
echo 'Guest Combination ' . $guest_combi . "\n";
echo json_encode($assignments, JSON_PRETTY_PRINT);
echo "\n\n";
}
The output will look something like this:
...
Guest Combination 13
[
[
"1 person room",
"2 person room with other",
"2 person room alone",
"2 person",
"3 person"
],
[
"3 person"
]
]
...
"Guest combination 13" means the 4 guests will be split into groups of 1 and 3 persons.
Output is an array of possible rooms for each group. So in the example, the group of 1 can book 1 person room, 2 person room with other, ... 3 person room. And the group of 3 can book 3 person room.
—
Other notes:
I know we hate global but doing this just for brevity. Feel free to modify.
There's a shorter way to code this, but this implementation makes it easier to debug since guest combinations are used as keys.
I have a series of columns (Jan, Feb, Mar, etc) and I want to average the values of each column for each row however many there may be.
I have:
protected function generateAverage($staff, $type)
{
$months = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
$staff = array_map(function ($row) use ($type) {
if ($row['row_type'] == $type) {
return $row;
}
}, $staff);
foreach ($staff as $key => $employee) {
$months[0] += $employee['amounts'][0];
$months[1] += $employee['amounts'][1];
$months[2] += $employee['amounts'][2];
$months[3] += $employee['amounts'][3];
$months[4] += $employee['amounts'][4];
$months[5] += $employee['amounts'][5];
$months[6] += $employee['amounts'][6];
$months[7] += $employee['amounts'][7];
$months[8] += $employee['amounts'][8];
$months[9] += $employee['amounts'][9];
$months[10] += $employee['amounts'][10];
$months[11] += $employee['amounts'][11];
}
$months = array_map(function ($value) use ($staff) {
return $value / (count($staff) / 2);
}, $months);
$months[] = array_sum($months);
return $months;
}
Here is a sample of the data that goes into the above function:
array:6 [
0 => array:4 [
"amounts" => array:13 [
0 => "30000.00"
1 => "30000.00"
2 => "30000.00"
3 => "30000.00"
4 => "30000.00"
5 => "30000.00"
6 => "30000.00"
7 => "30000.00"
8 => "30000.00"
9 => "30000.00"
10 => "30000.00"
11 => "30000.00"
12 => 360000.0
]
"image" => "test.jpg"
"row_name" => "Target"
"row_type" => "target"
]
...
Usage:
$data['aggregates']['Target average'] = $this->generateAverage(array_values($data['staff']), 'target');
I feel the way the average is calculated is messy, is there a better way to do this?
A couple of small footprint reductions
protected function generateAverage($staff, $type)
{
// create 12 months with 0 value
$months = array_fill(0, 12, 0);
// use array_filter instead of map
$staff = array_filter(function ($row) use ($type) {
return $row['row_type'] === $type;
}, $staff);
// do count outside loop
$staffCount = count($staff);
// loop employees and add up each month, dividing early
foreach ($staff as $employee) {
for ($i = 0; $i < 12; $i++) {
$months[$i] += $employee['amounts'][$i] / $staffCount;
}
}
return $months;
}
I dont know why you are dividing the staff count by 2 or why you are summing in the end, my function just gives an average per month.
Since you're using Laravel, most of the data you work with is collections. So, before converting a collection into an array, you could use avg() helper:
$collection->avg($key);
I think you can consider using array_colum,
1) array_column makes an array of all the values in a particular index position in a row
$column1 = array_column($employee, 0 );
$column2 = array_column($employee, 1 );
$column3 = array_column($employee, 2 );
.
.
.
2)Get the column count by count($columnX), where X is the index of the column
3) Using (1) and (2) calculate the average as needed
I found this thread about picking the closest/nearest value from an array based upon a known value. What about if one wants to pick the two nearest values from an array looking at the same say?
$rebates = array(
1 => 0,
3 => 10,
5 => 25,
10 => 35)
$rebates = array(
1 => 0,
3 => 10,
5 => 25,
10 => 35);
function getArrayNeighborsByKey($array, $findKey) {
if ( ! array_key_exists($array, $findKey)) {
return FALSE;
}
$select = $prevous = $next = NULL;
foreach($array as $key => $value) {
$thisValue = array($key => $value);
if ($key === $findKey) {
$select = $thisValue;
continue;
}
if ($select !== NULL) {
$next = $thisValue;
break;
}
$previous = $thisValue;
}
return array(
'prev' => $previous,
'current' => $select,
'next' => $next
);
}
See it!
By "two nearest" you mean the two smaller than or equal to the value of $items?
Anyway, starting from the answer to that other thread, which is
$percent = $rebates[max(array_intersect(array_keys($rebates),range(0,$items)))];
You can go to
$two_nearest = array_slice(array_intersect(array_keys($rebates),range(0,$items)), -2);
$most_near = $rebates[$two_nearest[1]];
$less_near = $rebates[$two_nearest[0]];
This can probably be reduced to an one-liner using array_map, but I think it's overdone already.
$rebates = array(
1 => 0,
3 => 10,
5 => 25,
10 => 35)
$distances = array();
foreach($rebates as $key=>$item) {
if ($key == 5) continue;
$distances = abs($rebates[5] - $item);
}
sort($distances, SORT_NUMERIC)
Now you have an array with all the items in the array with their distance to $rebates[5] sorted. So you can get the two closest ones.
Or three closest ones. Whatever.
Just keep in mind that 2 items can have the same distance.
Suppose you have an array "value => timestamp". The values are increasing with the time but they can be reset at any moment.
For example :
$array = array(
1 => 6000,
2 => 7000,
3 => 8000,
7 => 9000,
8 => 10000,
9 => 11000,
55 => 1000,
56 => 2000,
57 => 3000,
59 => 4000,
60 => 5000,
);
I would like to retrieve all the missing values from this array.
This example would return :
array(4,5,6,58)
I don't want all the values between 9 and 55 because 9 is newer than the other higher values.
In real condition the script will deal with thousands of values so it need to be efficient.
Thanks for your help!
UPDATE :
The initial array can be ordered by timestamps if it is easier for the algorithm.
UPDATE 2 :
In my example the values are UNIX timestamps so they would look more like this : 1285242603 but for readability reason I simplified it.
Here’s another solution:
$prev = null;
$missing = array();
foreach ($array as $curr => $value) {
if (!is_null($prev)) {
if ($curr > $prev+1 && $value > $array[$prev]) {
$missing = array_merge($missing, range($prev+1, $curr-1));
}
}
$prev = $curr;
}
You can do the following:
Keep comparing adjacent array keys. If
they are consecutive you do nothing.
If they are not consecutive then you
check their values, if the value has
dropped from a higher value to a
lower value, it means there was a
reset so you do nothing.
If the value has not dropped then it
is a case of missing key(s). All the
numbers between the two keys(extremes
not included) are part of the result.
Translated in code:
$array = array( 1 => 6000, 2 => 7000, 3 => 8000, 7 => 9000, 8 => 10000,
9 => 11000,55 => 1000, 56 => 2000, 57 => 3000, 59 => 4000,
60 => 5000,);
$keys = array_keys($array);
for($i=0;$i<count($array)-1;$i++) {
if($array[$keys[$i]] < $array[$keys[$i+1]] && ($keys[$i+1]-$keys[$i] != 1) ) {
print(implode(' ',range($keys[$i]+1,$keys[$i+1]-1)));
print "\n";
}
}
Working link
This gives the desired result array(4,5,6,58):
$previous_value = NULL;
$temp_store = array();
$missing = array();
$keys = array_keys($array);
for($i = min($keys); $i <= max($keys); $i++)
{
if(!array_key_exists($i, $array))
{
$temp_store[] = $i;
}
else
{
if($previous_value < $array[$i])
{
$missing = array_merge($missing, $temp_store);
}
$temp_store = array();
$previous_value = $array[$i];
}
}
var_dump($missing);
Or just use Gumbo's very smart solution ;-)