1

I've two arrays:

$rooms = [1,2,3,4,5,6,7,8,9];
$reserved_rooms = [4, 7]; 

I would like to print this array like this:

1
2
3
Room 4 is reserved
5
6
Room 7 is reserved

Her is my code:

$rooms = [1,2,3,4,5,6,7,8,9];
$reserved_rooms = [4, 7]; 

foreach($rooms as $key=>$val){
    foreach($reserved_rooms as $val2){
        if($val == $val2){
            echo $val2." room is reserved";
        }   
        else
            echo $val."<br>";
    }
}

The result is:

1
1
2
2
3
3
4 room is reserved4
5
5
6
6
7
7 room is reserved8
8
9
9
1
  • In reaction to the comments on the answers. Specify a dummy but correct example. This will make it easier to help you find the best solution. Iterating over arrays is time consuming and probably not necessary. Commented Jan 27, 2016 at 11:23

5 Answers 5

4

You need to move your echo outside the loop.

$rooms = [1,2,3,4,5,6,7,8,9];
$reserved_rooms = [4, 7]; 

foreach($rooms as $key=>$val){
    $isReserved = false; // add this

    foreach($reserved_rooms as $val2){
        if($val == $val2){
            $isReserved = true;
            break; // is reserved, no need to check the other values
        }
    }

    if ($isReserved) { // now decide whether it is reserved or not
        echo $val." room is reserved";
    } else {
        echo $val."<br>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use in_array() to check if a value exists in the array.

foreach($rooms as $room) {
   if( in_array($room, $reserved_rooms) ) {
      echo $room .' is reserved.' . PHP_EOL;
      continue;
   }
   echo $room . PHP_EOL;
}

https://eval.in/508706

6 Comments

yeah, you are right. but is it possible to do not using in_array() function?
...why do you not want to use a native function?
@ʰᵈˑ wouldn't continue also echo the second echo ? Sorry if I'm mistaken
No, continue will finish that iteration and move on to the next
My problem is I can't use in_array(), coz it's other type comparison, not exact value. I just give this example to explain more simple
|
1

In case you do not want to use in_array() function :

$rooms = [1,2,3,4,5,6,7,8,9];
$reserved_rooms = [4, 7]; 

array_map(function ($room) use ($reserved_rooms) { 
    foreach($reserved_rooms as $reserved_room) {
       if ($reserved_room == $room) { 
           print "Room $room is reserved\n";
           return;
       }
    }
    print "$room\n"; 
}, $rooms);

https://eval.in/508743

Comments

0

You should try this:

foreach($rooms as $key=>$val){
    if(in_array($val, $reserved_rooms){
        echo $val." room is reserved";
    }else{
        echo $val
    }
}

Comments

0
$rooms = [1,2,3,4,5,6,7,8,9];
$reserved_rooms = [4, 7]; 

foreach($rooms as $key=>$val){
    $flag = false;
    foreach($reserved_rooms as $val2){
        if($val == $val2){
            echo "Room ".$val2." is reserved<br />";
            $flag=true;
            break;
        }        
    }
    if($flag == false){
        echo $val."<br />";
    }
}

2 Comments

I like the part where it looks identical to stackoverflow.com/a/35035422/3000179. Plus, code-only answers are discouraged
Please have a look at the code that you think identical. The accepted answer give output like 1 2 3 4 room is reserved5 6 7 room is reserved8 9 And my code give output like 1 2 3 Room 4 is reserved 5 6 Room 7 is reserved 8 9 That exactly what you want to print as described your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.