Q:

PHP - How to insert new element in array avoiding duplicates?

I want to create a function that takes as input parameters an array $arr_previsioni, a value $previsione, and a number $pos indicating the position.
The function should:
1 - add the value $previsione if it does not already exist.
2 - If the value $previsione given in input is already present in the array, it must modify the value of $previsione so that it is not different from all the others and in the end add it to the array.
Essentially given an input x number I have to create an array of unique numbers with this priority:
if the x number I gave at the input is already present in the array then we need to change the x number (adding or subtracting something) to make it unique.

function aggiungiPrevisione($previsione,$pos,$arr_previsioni){

    echo '<br>';

    // if it is the first element of the array
    if($pos == 1){
        $arr_previsioni['PREVISIONE1'] = $previsione;
        return $arr_previsioni;
    }

    $numero_elementi = count($arr_previsioni);   

    foreach($arr_previsioni as $key=>$value){

        for($i=0; $i <= $numero_elementi+2; $i++){

            // Verifica se esiste
            if (in_array($previsione, $arr_previsioni)){
                // The same number was found in array
                if($previsione  > 45){
                    $previsione = $previsione - 13;
                } else {
                    $previsione = $previsione + 13;
                }
                $previsione = getNumeroGiocabile($previsione); // returns a number from 0 to 90

            } // end checking

        } // end for

    } // end foreach

    // Add $previsione in array
    $arr_previsioni['PREVISIONE'.$pos] = $previsione; // by Vincent Decaux

    return $arr_previsioni;

}

$previsione = makePrevisione(); // return number from 1 to 90
$arr_previsioni = array(); // initially empty
for($pos=1; $pos<=24; $pos++){
   $arr_previsioni = aggiungiPrevisione($previsione,$pos,$arr_previsioni);
} 

var_dump($arr_previsioni);

The function I created returns an array of 24 elements but some are the same as the others. Here are the values ​​of the array:
array(24) {
  ["PREVISIONE1"]=>
  int(30)
  ["PREVISIONE2"]=>
  int(71)
  ["PREVISIONE3"]=>
  int(22)
  ["PREVISIONE4"]=>
  int(1)
  ["PREVISIONE5"]=>
  int(67)
  ["PREVISIONE6"]=>
  int(51)
  ["PREVISIONE7"]=>
  int(35)
  ["PREVISIONE8"]=>
  int(14)
  ["PREVISIONE9"]=>
  int(72)
  ["PREVISIONE10"]=>
  int(57)
  ["PREVISIONE11"]=>
  int(11)
  ["PREVISIONE12"]=>
  int(76)
  ["PREVISIONE13"]=>
  int(41)
  ["PREVISIONE14"]=>
  int(40)
  ["PREVISIONE15"]=>
  int(39)
  ["PREVISIONE16"]=>
  int(37)
  ["PREVISIONE17"]=>
  int(34)
  ["PREVISIONE18"]=>
  int(29)
  ["PREVISIONE19"]=>
  int(42)
  ["PREVISIONE20"]=>
  int(55)
  ["PREVISIONE21"]=>
  int(55)
  ["PREVISIONE22"]=>
  int(55)
  ["PREVISIONE23"]=>
  int(55)
  ["PREVISIONE24"]=>
  int(55)
}

as you can see there are repeated values ​​(55).
I would like the function to insert the number of the $previsione as provided in case it did not already exist in the array, otherwise it would have to modify the value of $previsione (in order to obtain a number that is not already present) and add it to the array.
Let's see if I can make it easier to understand the problem.
By giving these input parameters:
$arr_previsione = (
  "PREVISIONE1"=>
  30,
  "PREVISIONE2"=>
  71,
  "PREVISIONE3"=>
  22,
  "PREVISIONE4"=>
  1,
  "PREVISIONE5"=>
  67,
  "PREVISIONE6"=>
  51,
  "PREVISIONE7"=>
  35);

$pos = 8;
$previsione = 22;
$arr_previsioni = aggiungiPrevisione($previsione8,8,$arr_previsioni);

The result that I can currently get with the function shown above could be this:
$arr_previsione = (
  "PREVISIONE1"=>
  30,
  "PREVISIONE2"=>
  71,
  "PREVISIONE3"=>
  22,
  "PREVISIONE4"=>
  1,
  "PREVISIONE5"=>
  67,
  "PREVISIONE6"=>
  51,
  "PREVISIONE7"=>
  35,
  "PREVISIONE8"=>
  22); // error

What I would rather get is:
$arr_previsione = (
  "PREVISIONE1"=>
  30,
  "PREVISIONE2"=>
  71,
  "PREVISIONE3"=>
  22,
  "PREVISIONE4"=>
  1,
  "PREVISIONE5"=>
  67,
  "PREVISIONE6"=>
  51,
  "PREVISIONE7"=>
  35,
  "PREVISIONE8"=>
  48); // after the change the new value entered should be 48 or any other number, it is enough that it is not already present in the array itself

I hope I explained myself

A:

It seems you're looking for something like this:
function aggiungiPrevisione($previsione,$pos,$arr_previsioni) 
{
    echo '<br>';

    // if it is the first element of the array
    if ($pos == 1) {
        $arr_previsioni['PREVISIONE1'] = $previsione;
        return $arr_previsioni;
    }

    // Make sure $previsione does not yet exist
    while (in_array($previsione, $arr_previsioni)) {
        $previsione = rand(0, 90);
    } 

    // Add $previsione in array
    $arr_previsioni['PREVISIONE'.$pos] = $previsione; 

    return $arr_previsioni;
}

Note that I only changed the middle part of this function. I have a simple while loop checking if the value $previsione is in the array $arr_previsioni. If it is a new random value between 0 and 90 is generated for $previsione, and the loop condition is checked again, until the value is not present in the array anymore.
Note that this routine will fail as soon as all values between 0 and 90 have been used.
I also cannot correct all the other problems, like:

Having an echo '<br>'; in a function that's meant to manipulate an array.
Having unneeded string keys in the array. The default numeric keys would probably do.
Bad initialization routine for the array.
I like to program in English since the programming language is English. Mixing two languages doesn't help other people reading your code, unless they happen to be Italian. I'm Dutch, by the way. You wouldn't like to read Dutch code, I'm sure. Contrary to that your comments are in English. Were they added for the question only?

