Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:00:48+00:00 2026-06-12T12:00:48+00:00

Having some difficulty trying to do this. I’m fetching an array of all submissions,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:00:50+00:00Added an answer on June 12, 2026 at 12:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some difficulty with preg_match. I'm trying to match roman numerals, like this:
I'm trying to code this expression in python but I'm having some difficulty. This
im having some difficulty trying to pull out a specific value from a cookie.
I am trying new things with arrays and having some difficulty. I am trying
I'm trying to accomplish a task, but having some difficulty.Can someone set me straight
I am having some difficulty converting this list of strings into a list of
I am having some difficulty returning all the results I would expect when leaving
I'm having some difficulty with this example: <img src=image1/<?php echo $file; ?>.jpg style=width:500px />
I'm having some difficulty writing this code in php. I want to create a
I'm trying to parse an mrss feed using jquery but am having some difficulty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.