Check if number is available within a defined range

1

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

Now I need it 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 an orientation on how to continue?

    
asked by anonymous 04.10.2018 / 16:49

1 answer

2

The ideal is to convert these two strings into two arrays with explode() , so you can get the difference (the elements of $todos that 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);
    
04.10.2018 / 19:16