donxbje866688
donxbje866688
2012-10-07 21:00

Foreach通过数组,在数组中进行一些计算,然后创建新数组并合并回原始数组

已采纳

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dtef9322 dtef9322 9年前

    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.

    点赞 评论 复制链接分享
  • doublestar2014 doublestar2014 9年前

    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;
    
    点赞 评论 复制链接分享
  • du9757 du9757 9年前

    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;
    }
    
    点赞 评论 复制链接分享

相关推荐