Jump to content

Return null or array in function if conditions are met


jiros1
Go to solution Solved by cyberRobot,

Recommended Posts

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.

Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

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;
Link to comment
Share on other sites

  • Solution

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;
}
 
...//
?>
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.