Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a code that gives me exactly the right output, but I keep getting errors during the process, and I'm not allowed to use @ to stop the errors. :(

$som = '';
foreach ($prizeStr as $som=>$ptext) {
$Cloth = $AssocArr[$som+1];
//doing something else here etc...
}

I have an array of prize money ($prizeStr), it's unknown how many amounts of money there will be. I need each place getter to have a prize amount until there's none left. I realise that $ptext is each prize amount, and $som is the key, then I've converted the key into the associative array, and found the Number of the runner. I can't figure out a way to get rid of that error message that gives me "undefined offset" errors.

I've tried

$AssocArr[$som]++;

and that doesn't work, I've tried isset, but that gives me the same issue. I've also tried to put it as:

$AssocArr[$som];
$som++;

But nothing is working... Any ideas you guys have would be great. Let me know if you need more info... and Thanks in advance!

EDIT: I have (for example) I pregmatched something to create my associative array ended up with:

$position = 1;
$AssocArr[$position] = $cloth;
$position++;

essentially giving me:

$AssocArr = array(1=>2,2=>4,3=>15,4=>20,5=>17,6=>1,7=>6,8=>12);

I pregmatched again to get the prize string of:

$prizeStr = array('11.40', '5.00', '2.90', '1.80', '3.50');

Then I get to the error part. Thing is, the above prize string could have 5 values or 10 values or whatever. What I need to do is have 1 prize amount per winner, which the code does, it just gives me an illegal offset, which I'm trying to get rid of. That's what I'm trying to do.

foreach($prizeStr as $som=>$ptext) {
$Cloth = $AssocArr[$som+1];
// this finds the position keys and increases it by 1.

end result should be winner: 2 = 11.40, then places: 2 = 5.00, 4 = 2.90, 15 = 1.80 etc... Does that make more sense? I hope that helps more. Thanks!

EDIT: In doing more testing, I realise that I need to build the array before the code can continue. I'm getting the offset error because my array isn't complete before my code tries to call the key it needs. Can anyone help me build it before I do the rest?

share|improve this question

closed as unclear what you're asking by kapa, squeamish ossifrage, Robby Cornelissen, La-comadreja, Ghost Sep 18 '14 at 3:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
what do you want to find ? –  Deepak Nov 5 '13 at 3:57

1 Answer 1

Although your variables are all over the place, and I am not sure where you are assigning them, this is the logic that I would use:

<?php

    $maxPrize=10000;
    // The Maximum amount of prizemoney that you can give away.

    $prizeAmount=100;
    // The amount of cash for each winner.
    $winnerArray=array();

    foreach($prizeStr as $winner)
    {
        if($maxPrize>=$prizeAmount)
        // Check to make sure we have money left over.
        {
            $maxPrize-=$prizeAmount;
            // subtract the amount of money from the pool.
            $winnerArray[]=array('WinnerID' => $winner, 'PrizeMoney' => $prizeAmount);
            // Keep track of who won what.
        }
        else
        {
            break;
            // no point in checking any other array elements.
        }
    }
    print_r($winnerArray);
    // Show final array of winners.
    echo "The amount left over: ".$maxPrize;
    // Show if any funds are left over.
?>
share|improve this answer
    
Unfortunately I'm not looking for the maximum prize amount, as that information isn't given to me. Thanks anyway... –  Kozbot Nov 5 '13 at 4:48
    
The $maxPrize was how much prize money you started with and the logic was to allow the following from your question "it's unknown how many amounts of money there will be. I need each place getter to have a prize amount until there's none left" –  Fluffeh Nov 5 '13 at 4:49
    
Sorry, I meant no other amounts given. That was my mistake... –  Kozbot Nov 5 '13 at 4:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.