Check if number is available within a defined range

Asked

Viewed 134 times

1

I have a pre-defined numbering, for example from 1 to 4.

Now I need him to show the numbers that are not being used, which would be:

Números não usados: 2,4

I started the code like this:

<?php 
$numeros_sorteio="1,2,3,4";
$numeros_usados="1,3";

echo "numeros livres: 2,4";

?>

Could someone give me some guidance on how to continue?

  • What is this little bit of PHP you know? With it, you’ve managed to at least try something?

  • By the way, welcome to the site. Seek to do the [tour], access [help] and read the guide of [Ask] to orient yourself with the basics of the operation of the site. If you have any doubt, you can attend the [meta].

  • Hello I apologize to the colleagues if I asked wrong I kindly do not deny me edit the question if it is the case I’m starting now thousand apologies there fixing the question

  • The will :D All actions here on the site are reversible. The negative vote is only one way to show that there is something wrong with your question, so much so that the description on the button is "this question shows no research effort; it is not clear or not useful". You can edit it and if you stay within the standards, who voted negatively can review the vote, withdrawing the negative vote and even giving the positive.

  • Thanks understood let’s go edit there will be cool I say clearer ?

  • I’ll give you the logic to study: create a array with the numbers listed and the other with free, then you must go through this array with a for and compare one with the other using a if. Note: In PHP there is a comparison of == (equal) and === (identical), for example: 1 == "1", This means that the values are equal, but not identical (see that they are of different types), already 1 === 1 are equal and identical.

Show 1 more comment

1 answer

2

The ideal is to convert these two strings into two arrays with explode(), this way can obtain the difference (the elements of $todos who are not in $disponiveis) with the function array_diff()

$todos = explode(',', '1,2,3,4');
$usados = explode(',','1,3');

$disponiveis = array_diff($todos, $usados);
echo 'Números disponiveis: '. implode(',', $disponiveis);
  • 2

    Perfect worked out Thanks there to all thank you very much !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.