bob_cobb
bob_cobb

Reputation: 2269

Foreach through array, do some calculations within the array, then create new array and merge back into original

Having some difficulty trying to do this. I'm fetching an array of all submissions, and they are being returned in an array like this:

Array
(
    [0] => Array
        (
            [Submission] => Array
                (
                    [user_id] => 2
                    [title] => This is a test, only a test
                    [source] => http://www.testing.com
                    [slug] => this-is-a-test-only-a-test
                    [category] => misc
                    [created] => 2012-10-05 03:43:11
                    [id] => 110
                )

            [SubmissionsVote] => Array
                (
                    [0] => Array
                        (
                            [id] => 336
                            [user_id] => 2
                            [submission_id] => 110
                            [vote_type] => up
                        )

                )
                (
                    [1] => Array
                        (
                            [id] => 337
                            [user_id] => 4
                            [submission_id] => 110
                            [vote_type] => down
                        )

                )
        )
)

My intent is to loop through that returned array, and grab all the SubmissionsVote by vote_type == up and vote_type == down.

I then want to calculate a score (this example would return a score of 0 which is just: up - down. With that, I want to put that score into an array and append it to the end of the original one so it'd look like:

Array
(
    [0] => Array
        (
           [Submission] => Array
                (
                    [user_id] => 2
                    [title] => This is a test, only a test
                    [source] => http://www.testing.com
                    [slug] => this-is-a-test-only-a-test
                    [category] => misc
                    [created] => 2012-10-05 03:43:11
                    [id] => 110
                )

            [SubmissionsVote] => Array
                (
                    [0] => Array
                        (
                            [id] => 336
                            [user_id] => 2
                            [submission_id] => 110
                            [vote_type] => up
                        )

                )
                (
                    [1] => Array
                        (
                            [id] => 337
                            [user_id] => 4
                            [submission_id] => 110
                            [vote_type] => down
                        )

                )
        [SubmissionScore] => Array
        (
             [0] => 0
        )
        )
)

Here is what I am doing unsuccessfully:

    $votes = array();
    $totalScore = array();

    foreach ($submissions as $submission) {
        $vote = $submission['SubmissionsVote'];
        array_push($votes, $vote);
    }   

    for ($i = 0; $i < count($votes); $i++) {
        $downVotes = 0;
        $upVotes = 0;

        if ($votes[$i]['vote_type'] == 'down') {
            $downVotes += 1;
        } else if ($votes[$i]['vote_type'] == 'up') {
            $upVotes += 1;
        }   

        $score = $upVotes - $downVotes;
        $totalScore = array_push($totalScore, $score);
    }

Would love to get a little push in the right direction here.

Upvotes: 0

Views: 168

Answers (3)

Chester Alan
Chester Alan

Reputation: 81

Try this one:

$newSubmissions = array();

foreach ($submissions as $submission) {
    $SubmissionsVote = $submission['SubmissionsVote'];

    foreach($SubmissionsVote as $votes) {
        $score = 0;
         if ($votes['vote_type'] == 'down') {
            $score -= 1;
        } else if ($votes['vote_type'] == 'up') {
            $score += 1;
        }   
        $submission['SubmissionScore'] = array($score);
    }
    $newSubmissions[] = $submission;

}

$submissions = $newSubmissions;

Upvotes: 3

Satevis
Satevis

Reputation: 1348

I see at least two errors in your code :

  • You never set SubmissionScore in your array to $totalScore value.
  • Even if you add a $votes[$i]['SubmissionScore'] = $totalScore, that will not work, in PHP only object are passed by reference so $votes will only contain copy.

There are multiple possibility to resolve the problem but a reference in the foreach is a good choice :

foreach ($submissions as &$submission) {
    $totalScore = 0;
    foreach ($submission['SubmissionsVote'] as $vote) {
        if ($vote['vote_type'] == 'down') {
            $totalScore--;
        } else if ($vote['vote_type'] == 'up') {
            $totalScore++;
        }
    }
    $submission['SubmissionScore'] = $totalScore;
}

Each submission will now have a SubmissionScore based on up/down vote.

Upvotes: 1

Aljana Polanc
Aljana Polanc

Reputation: 950

You are initializing $downVotes and $upVotes inside a loop, that means you override values each time you iterate through the array.

Anyway, you could simplify this.

foreach ($submissions as $index => $submission) {
    $up = 0;
    $down = 0;
    foreach ($submission['SubmissionsVote'] as $vote) {
        if ($vote['vote_type'] == 'up') {
            $up++;
        } else {
            $down++;
        }
    }
    $submissions[$index]['SubmissionScore'] = $up - $down;
}

Upvotes: 1

Related Questions