How can received and clear an array in PHP

Solution:

$div = [];

$maxDivKey = false;
$maxDiv = 0;

for($i = 1; $i <= 10000; $i++) {
    for ($j = 1; $j < $i; $j++){
             
        if ($i % $j == 0){
            $div[$i][] = $i.'/'.$j.'='.$i/$j;
        }
        
        if($j == $i-1){
            $count = count($div[$i]);
            $div[$i]['count'] = $count;
            if($maxDiv < $count){
                $maxDiv = $count;
                $maxDivKey = $i;
            }
        }
    }
}


echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);

Answer

Solution:

I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.

<?php

$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();

$max_divs_arr=array();


for($i=$start_num;$i<=$max_num;$i++)
{

        $divs=0;
        $div_array=array();
        for($j=$start_num;$j<=$i;$j++)
        {
                if($i%$j==0)
                {
                        $divs++;
                        $div_array[]=$j;
                }
        }



        if($divs==$max_divs)
        $max_divs_arr[$i]=$div_array;

        if($divs>$max_divs)
        {
                $max_divs_arr=array();
                $max_divs=$divs;
                $max_divs_arr[$i]=$div_array;
        }


}



foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

Source