jiros1 Posted January 23, 2016 Share Posted January 23, 2016 I'm trying to make a function that checks if there are enough seats available for visitors. If there's enough room for the visitors, return an array with suggested seats, else return null. Method: I'm making a gap, this gap will be increased by 1 everytime there is a available seat. At the end of my function, I compare if the $visitors fit in the $gap, if they don't return null and if they do, return the array. The problem: My function returns an array even though there's not enough room. My question: How do I let my function return null if there are not enough seats? This is my code: <?php error_reporting(E_ALL & ~E_NOTICE); $visitors = 6; $seats = array( array( 'seatAvailable' => '0', 'seatNumber' => '1' ), array( 'seatAvailable' => '0', 'seatNumber' => '2' ), array( 'seatAvailable' => '1', 'seatNumber' => '3' ), array( 'seatAvailable' => '1', 'seatNumber' => '4' ), array( 'seatAvailable' => '1', 'seatNumber' => '5' ), ); $gap = 0; echo "Visitors: ".$visitors; echo "<ol>"; function suggestSeats($seats,$visitors){ foreach($seats as &$val){ //If available if($val['seatAvailable'] == '1'){ //If there's no gap, start making one now: if($gap<1){ $gap = 1; } $gap++; //IF THERE ARE STILL VISITORS TO PROCESS, MARK THE SUGGESTED SEAT if($visitors > 0) { $val['seatAvailable'] = 'x'; $visitors--; } } //IF THERE ARE NO MORE VISITORS, EXIT LOOP if($visitors == 0) { break; } } //echo "gap".$gap; //If the visitors don't fit in the gap of available seats. if($visitors>$gap){ $result = null; } else{ $result = $seats; } return $result; } echo "</ol>"; echo "<pre>"; print_r (suggestSeats($seats,$visitors)); echo "</pre>"; ?> This is the output of my script as it is now: Visitors: 6 Array ( [0] => Array ( [seatAvailable] => 0 [seatNumber] => 1 ) [1] => Array ( [seatAvailable] => 0 [seatNumber] => 2 ) [2] => Array ( [seatAvailable] => x [seatNumber] => 3 ) [3] => Array ( [seatAvailable] => x [seatNumber] => 4 ) [4] => Array ( [seatAvailable] => x [seatNumber] => 5 ) ) I want it to show null instead of returning the array. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 23, 2016 Share Posted January 23, 2016 It would appear that your script is doing that for you now. But since your output is showing an array apparently your test conditions are producing valid output. Have you tried testing with conditions that would ensure that you won't get any data back? IMHO - wouldn't it be easier to understand/handle if you returned 'false' instead of a null value? 1 Quote Link to comment Share on other sites More sharing options...
jiros1 Posted January 24, 2016 Author Share Posted January 24, 2016 It appears that the condition is set right, because if I work with only strings then it works just fine. But when I return null or an array then it doens't work... I just can't get my head around this, what am I missing? //If the visitors can't fit in the gap of available seats. if($visitors<$gap){ //$result = false; $result = "doesn't fit"; } else{ //$result = $seats; $result = "it fits"; } return $result; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 24, 2016 Share Posted January 24, 2016 What do you mean "when you return null it doesn't work"? WHat doesn't work - the return? Or the "print_r" that you are doing? For a solution to the latter simply CHECK the value that is returned before trying to print it. Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted January 25, 2016 Solution Share Posted January 25, 2016 If you echo out $visitors before the if test that changes $result to NULL, you'll see that the value has been changed. <?php //... echo "<div>$visitors</div>"; //If the visitors don't fit in the gap of available seats. if($visitors>$gap){ $result = NULL; } //... ?> One way around this is to create a temporary variable which will be used in the loop. <?php //... function suggestSeats($seats,$visitors){ $visitorsToProcess = $visitors; foreach($seats as &$val){ //If available if($val['seatAvailable'] == '1'){ //If there's no gap, start making one now: if($gap<1){ $gap = 1; } $gap++; //IF THERE ARE STILL VISITORS TO PROCESS, MARK THE SUGGESTED SEAT if($visitorsToProcess > 0) { $val['seatAvailable'] = 'x'; $visitorsToProcess--; } } //IF THERE ARE NO MORE VISITORS, EXIT LOOP if($visitorsToProcess == 0) { break; } } echo "<div>Visitors before loop: $visitors</div>"; echo "<div>Visitors after loop: $visitorsToProcess</div>"; echo "<div>Gap: $gap</div>"; //If the visitors don't fit in the gap of available seats. if($visitors>$gap){ $result = NULL; } else{ $result = $seats; } return $result; } ...// ?> 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.