I wrote this function in PHP. It divides each variable $votesX, where X is a politic party name, for 1,2,3... to the value of $seats. Now I've to search the the biggest values of the array and replace them with a zero. How can I do it?

function hondt ($votesA,$votesB,$votesC,$votesD,$votesE,$seats){
for ($i=1; $i<=$seats; $i++){
$votes[0][$i]=$votesA/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[1][$i]=$votesB/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[2][$i]=$votesC/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[3][$i]=$votesD/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[4][$i]=$votesE/$i;
}
}

I need it urgently.

Thank you in advance.

Recommended Answers

All 8 Replies

Now I've to search the the biggest values of the array and replace them with a zero. How can I do it???

Can you give one example with live values?

I think you can do the following:

function hondt ($votesA,$votesB,$votesC,$votesD,$votesE,$seats)
{
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[0][$i]=$votesA/$i;
        $votesMax[0] = ($votes[0][$i] > $votesMax[0])? $votes[0][$i] : $votesMax[0];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[1][$i]=$votesB/$i;
        $votesMax[1] = ($votes[1][$i] > $votesMax[1])? $votes[1][$i] : $votesMax[1];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[2][$i]=$votesC/$i;
        $votesMax[2] = ($votes[2][$i] > $votesMax[2])? $votes[2][$i] : $votesMax[2];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[3][$i]=$votesD/$i;
        $votesMax[3] = ($votes[3][$i] > $votesMax[3])? $votes[3][$i] : $votesMax[3];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[4][$i]=$votesE/$i;
        $votesMax[4] = ($votes[4][$i] > $votesMax[4])? $votes[4][$i] : $votesMax[4];
    }
    for ($n=0; $n <= 4; $n++)
    {
        $TheVotesMax = ($votesMax[$i] > $TheVotesMax)? $votesMax[$i] : $TheVotesMax[$i];
    }
}

And I think that the following code will do the best briefly:

function hondt ($votesA,$votesB,$votesC,$votesD,$votesE,$seats)
{
    $Names = array($votesA,$votesB,$votesC,$votesD,$votesE);
    for($ii=0; $ii <= 5; $ii++)
    {
        for ($i=1; $i<=$seats; $i++)
        {
            $votes[$ii][$i]=$Names[$ii]/$i;
            $votesMax[$ii] = ($votes[$ii][$i] > $votesMax[$ii])? $votes[$ii][$i] : $votesMax[0];
        }
    }
    for ($i=0; $i <= 4; $i++)
    {
        $TheVotesMax = ($votesMax[$i] > $TheVotesMax)? $votesMax[$i] : $TheVotesMax[$i];
    }
}

Wish it's done what you want. :)

you need to define $votes as an array
like

$votes = array();

you need to define $votes as an array

You can try it.. It works perfectly fine (100%) without defining it as an array...
PHP is an easy language that doesn't oblige you to type every single detail (With no excess).

okay i allways define everything then later i dont get any problem

make it work make it nice make it fast

And this is how to make it fast! :)

yh okay but first tings are gonna work

I tried this code and it worked perfectly. Has it worked perfectly with you, vibhadebit?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.