0

I might be punching slightly above my weight here or maybe its a really simple problem with a simple solution.

MY PROBLEM

  1. I cant access array key values in foreach()
  2. When creating the array in a function it seems that the array is duplicating, does a double iteration when created(as you can see in image below)

enter image description here

STEP 1: Array Creation

     $results = $stmnt->fetchAll();
            if ($stmnt->rowCount() > 0) {
                $totalCorrectPicks = 0;
//--->HERE ARRAY KEYS ARE CREATED FROM QUERYING DB
                foreach ($results as $index => $result) {
                    $returnResult = array('picked' => $result['team'], 'homeScore' => $result['homeScore'], 'awayScore' => $result['awayScore'], 'homeTeam' => $result['homeTeam'],
                        'awayTeam' => $result['awayTeam'], 'score' => $result['score']);
                }//end foreach
//------> HERE ELEMENTS GETS APPENDED TO ARRAY
                    $pickedTeam = $result['team'];
                    if ($result['homeScore'] > $result['awayScore']) {
                        $matchOutcome = $result['homeTeam'];
                        $matchScore = $result['homeScore'];
                        $returnResults['matchOutcome'] = $matchOutcome;
                        $returnResults['matchScore'] = $matchScore;
                    }
                    if ($result['awayScore'] > $result['homeScore']) {
                        $matchOutcome = $result['awayTeam'];
                        $matchScore = $result['awayScore'];
                        $returnResults['matchOutcome'] = $matchOutcome;
                        $returnResults['matchScore'] = $matchScore;
                    }
                    if ($pickedTeam === $matchOutcome) {
                        $totalCorrectPicks++;
                        $margin = abs($matchScore - $result['points']);
                        //INDEX WILL START AT 0 SO WE ADD ONE TO $INDEX
                        $nrGames = $index + 1;
                        $returnResults['totatlCorrectPicks'] = $totalCorrectPicks;
                        $returnResults['margin'] = $margin;
                        $returnResults['nrGames'] = $nrGames;
                    }
                    elseif ($pickedTeam !== $matchOutcome) {
                        $margin = 'wrongPick';
                        $returnResults['margin'] = $margin;
                }
            }
        }
        if(isset($returnResults)){
            print_r($returnResults);
            return $returnResults;
        }
        return false;
    }

STEP 2 Calling function and using array; leads to illegal string offset

 <?php $picks = checkUserPicks('5');
        foreach ($picks as $index => $pick){
        ?>
        <tr>
            <td>
            <?php echo $pick['picked']; ?>
            </td>
            <td>
             <?php echo $pick['matchOutcome']; ?>
            </td>
            <td>
                <?php echo $pick['margin']; ?>
            </td>
        </tr>
        <?php } ?>

NOTE: you can see the array value in the image posted above.

Questions

1) Why does it seem like the array is duplicated (see image)?

2) How can I fix the illegal string offset and what causes it?

Thanks

UPDATE

New Array Structure after removing [] from $returnResults[]

enter image description here

1 Answer 1

1

After end of foreach loop, matchOutcome, matchScore, etc are appended to the array. Shouldn't it be inside the loop?

If you are expecting more than one row, then you need to create array by maintaining its index as follows:

$returnResults = array(); // Initialize array
foreach ($results as $index => $result) {
    $returnResults[$index] = array('picked' => $result['team'], 'homeScore' => $result['homeScore'],
    'awayScore' => $result['awayScore'], 'homeTeam' => $result['homeTeam'],
    'awayTeam' => $result['awayTeam'], 'score' => $result['score']);

    //------> HERE ELEMENTS GETS APPENDED TO ARRAY
    $pickedTeam = $result['team'];
    if ($result['homeScore'] > $result['awayScore']) {
        $matchOutcome = $result['homeTeam'];
        $matchScore = $result['homeScore'];
        $returnResults[$index]['matchOutcome'] = $matchOutcome;
        $returnResults[$index]['matchScore'] = $matchScore;
    }
    .
    .
    elseif ($pickedTeam !== $matchOutcome) {
        $margin = 'wrongPick';
        $returnResults[$index]['margin'] = $margin;
    }
}//end foreach
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks Samir Ill have a quick check and let you know
No unfortunately it did not solve the problem still illegal string offset and duplicate array problem, however the array structure has changed, have a look at updated question.
do you expect multiple rows to be returned from query?
I do expect multiple rows yes.
ok. Understood the problem now. Check my updated answer. Also it seems you have printed array twice in your code due to which it's visible twice.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.