0

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

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==获取所有 SubmissionsVote down

然后我想计算一个分数(这个例子将返回一个分数,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);
    }

很想在这里朝着正确的方向前进。

4

3 回答 3

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;
于 2012-10-07T21:32:28.000 回答
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;
}
于 2012-10-07T21:29:40.067 回答
1

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

  • 你从来没有SubmissionScore在你的数组中设置$totalScore值。
  • 即使你添加一个$votes[$i]['SubmissionScore'] = $totalScore,那也不起作用,在 PHP 中只有对象是通过引用传递的,所以$votes只会包含副本。

有多种解决问题的可能性,但在 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基于赞成/反对的投票。

于 2012-10-07T21:29:52.953 回答