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

时间:2012-10-07 21:00:22

标签: php multidimensional-array foreach

尝试这样做有些困难。我正在获取所有提交的数组,并且它们将以如下数组返回:

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
                        )

                )
        )
)

我的目的是遍历返回的数组,并通过vote_type == upvote_type == down获取所有SubmissionsVote。

然后我想计算一个分数(这个例子会得到0的分数,它只是:up - down。有了这个,我想把这个分数归入数组并将其附加到原始数组的末尾,所以它看起来像:

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
        )
        )
)

以下是我的工作失败:

    $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);
    }

很想在这里朝着正确的方向努力。

3 个答案:

答案 0 :(得分:3)

试试这个:

$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;

答案 1 :(得分:1)

您正在循环中初始化$ downVotes和$ upVotes,这意味着每次遍历数组时都会覆盖值。

无论如何,你可以简化这个。

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;
}

答案 2 :(得分:1)

我在代码中看到至少两个错误:

  • 您从未将数组中的SubmissionScore设置为$totalScore值。
  • 即使您添加了$votes[$i]['SubmissionScore'] = $totalScore,但这不起作用,在PHP中,只有对象通过引用传递,因此$votes只包含副本。

解决问题有多种可能性,但reference in the foreach是一个不错的选择:

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;
}

现在每次提交都会根据上/下投票获得SubmissionScore