Note that there are some explanatory texts on larger screens.

plurals
  1. POForeach through array, do some calculations within the array, then create new array and merge back into original
    primarykey
    data
    text
    <p>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:</p> <pre><code>Array ( [0] =&gt; Array ( [Submission] =&gt; Array ( [user_id] =&gt; 2 [title] =&gt; This is a test, only a test [source] =&gt; http://www.testing.com [slug] =&gt; this-is-a-test-only-a-test [category] =&gt; misc [created] =&gt; 2012-10-05 03:43:11 [id] =&gt; 110 ) [SubmissionsVote] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 336 [user_id] =&gt; 2 [submission_id] =&gt; 110 [vote_type] =&gt; up ) ) ( [1] =&gt; Array ( [id] =&gt; 337 [user_id] =&gt; 4 [submission_id] =&gt; 110 [vote_type] =&gt; down ) ) ) ) </code></pre> <p>My intent is to loop through that returned array, and grab all the SubmissionsVote by <code>vote_type</code> == <code>up</code> and <code>vote_type</code> == <code>down</code>. </p> <p>I then want to calculate a score (this example would return a score of <code>0</code> which is just: <code>up</code> - <code>down</code>. 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:</p> <pre><code>Array ( [0] =&gt; Array ( [Submission] =&gt; Array ( [user_id] =&gt; 2 [title] =&gt; This is a test, only a test [source] =&gt; http://www.testing.com [slug] =&gt; this-is-a-test-only-a-test [category] =&gt; misc [created] =&gt; 2012-10-05 03:43:11 [id] =&gt; 110 ) [SubmissionsVote] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 336 [user_id] =&gt; 2 [submission_id] =&gt; 110 [vote_type] =&gt; up ) ) ( [1] =&gt; Array ( [id] =&gt; 337 [user_id] =&gt; 4 [submission_id] =&gt; 110 [vote_type] =&gt; down ) ) [SubmissionScore] =&gt; Array ( [0] =&gt; 0 ) ) ) </code></pre> <p>Here is what I am doing unsuccessfully:</p> <pre><code> $votes = array(); $totalScore = array(); foreach ($submissions as $submission) { $vote = $submission['SubmissionsVote']; array_push($votes, $vote); } for ($i = 0; $i &lt; 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); } </code></pre> <p>Would love to get a little push in the right direction here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload