Jump to content

jarvis

Members
  • Content Count

    443
  • Joined

  • Last visited

Community Reputation

1 Neutral

About jarvis

  • Rank
    Advanced Member

Profile Information

  • Gender
    Not Telling
  1. jarvis

    Incorrect Total In Array

    @Barand Once again, thank you so much!
  2. jarvis

    Incorrect Total In Array

    Hi @Barand That worked a charm! $results = []; foreach ($result as $year => $value) { if (!isset($results[$year])) { $results[$year] = []; } foreach ($value as $value) { if (!isset($results[$year][$value['team']])) { $results[$year][$value['team']] = 0; } $results[$year][$value['team']] += $value['value']; } } echo '<pre>', print_r($results, 1), '</pre>'; May I ask, how would I best convert that to a table output? - Sorry, cheeky I know
  3. jarvis

    Incorrect Total In Array

    Hi All, I'm at a loss on this and think it's something glaringly obvious. Basically, I've got the following array: rray ( [2016] => Array ( [0] => Array ( [team] => McLaren [value] => 0.4 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 1 ) [11] => Array ( [team] => McLaren [value] => 1 ) [12] => Array ( [team] => McLaren [value] => 1 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 2 ) [16] => Array ( [team] => McLaren [value] => 2 ) [17] => Array ( [team] => McLaren [value] => 2 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 2 ) [21] => Array ( [team] => McLaren [value] => 2 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 3 ) [24] => Array ( [team] => McLaren [value] => 10 ) [25] => Array ( [team] => McLaren [value] => 15 ) [26] => Array ( [team] => McLaren [value] => 25 ) [27] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [36] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [37] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 1 ) [41] => Array ( [team] => Red Bull Racing [value] => 1 ) [42] => Array ( [team] => Red Bull Racing [value] => 3 ) [43] => Array ( [team] => Red Bull Racing [value] => 3 ) [44] => Array ( [team] => Red Bull Racing [value] => 5 ) [45] => Array ( [team] => Red Bull Racing [value] => 5 ) [46] => Array ( [team] => Red Bull Racing [value] => 5 ) [47] => Array ( [team] => Red Bull Racing [value] => 7 ) [48] => Array ( [team] => Red Bull Racing [value] => 8 ) [49] => Array ( [team] => Red Bull Racing [value] => 20 ) [50] => Array ( [team] => Red Bull Racing [value] => 30 ) ) [2017] => Array ( [0] => Array ( [team] => McLaren [value] => 0.2 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 0.5 ) [11] => Array ( [team] => McLaren [value] => 1 ) [12] => Array ( [team] => McLaren [value] => 1 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 1 ) [16] => Array ( [team] => McLaren [value] => 1 ) [17] => Array ( [team] => McLaren [value] => 1 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 3 ) [21] => Array ( [team] => McLaren [value] => 3 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 3 ) [24] => Array ( [team] => McLaren [value] => 5 ) [25] => Array ( [team] => McLaren [value] => 10 ) [26] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [27] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [36] => Array ( [team] => Red Bull Racing [value] => 1 ) [37] => Array ( [team] => Red Bull Racing [value] => 1 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 3 ) [41] => Array ( [team] => Red Bull Racing [value] => 3 ) [42] => Array ( [team] => Red Bull Racing [value] => 4 ) [43] => Array ( [team] => Red Bull Racing [value] => 5 ) [44] => Array ( [team] => Red Bull Racing [value] => 7 ) [45] => Array ( [team] => Red Bull Racing [value] => 8 ) [46] => Array ( [team] => Red Bull Racing [value] => 20 ) [47] => Array ( [team] => Red Bull Racing [value] => 30 ) ) [2018] => Array ( [0] => Array ( [team] => McLaren [value] => 0.25 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 0.4 ) [11] => Array ( [team] => McLaren [value] => 0.5 ) [12] => Array ( [team] => McLaren [value] => 0.5 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 1 ) [16] => Array ( [team] => McLaren [value] => 1 ) [17] => Array ( [team] => McLaren [value] => 1 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 3 ) [21] => Array ( [team] => McLaren [value] => 3 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 4 ) [24] => Array ( [team] => McLaren [value] => 5 ) [25] => Array ( [team] => McLaren [value] => 5 ) [26] => Array ( [team] => McLaren [value] => 10 ) [27] => Array ( [team] => McLaren [value] => 10 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.2 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [36] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [37] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 1 ) [41] => Array ( [team] => Red Bull Racing [value] => 1 ) [42] => Array ( [team] => Red Bull Racing [value] => 1 ) [43] => Array ( [team] => Red Bull Racing [value] => 3 ) [44] => Array ( [team] => Red Bull Racing [value] => 4 ) [45] => Array ( [team] => Red Bull Racing [value] => 5 ) [46] => Array ( [team] => Red Bull Racing [value] => 7 ) [47] => Array ( [team] => Red Bull Racing [value] => 20 ) [48] => Array ( [team] => Red Bull Racing [value] => 30 ) [49] => Array ( [team] => Red Bull Racing [value] => 40 ) ) ) I then have the following code which outputs a table: $total_value_by_team = array(); foreach ($result as $year => $total) { $runningSum = 0; foreach($total as $key => $value){ if (is_numeric ($value['value'])): $runningSum += $value['value']; else: $runningSum += '0'; endif; $total_value_by_team[] = array( $value['team'], $year, $runningSum); } } $team_names = array(); foreach($total_value_by_team as $record): if(!in_array($record[0], $team_names)) : $team_names[] = $record[0]; endif; endforeach; sort($team_names); #Loop through records to create structured array $outputData = array(); foreach($total_value_by_team as $record): if(!isset($outputData[$record[1]])) : $outputData[$record[1]] = array_fill_keys($team_names, ''); endif; $outputData[$record[1]][$record[0]] = $record[2]; endforeach; #Create headers for table $headers = "<th></th>\n"; foreach($team_names as $team_name): $headers .= "<th>{$team_name}</th>\n"; endforeach; #Create the content for output $output = ''; foreach($outputData as $team => $team_names): $output .= "<tr>\n"; $output .= "<th>{$team}</th>\n"; foreach($team_names as $team_name => $value): $output .= "<td>{$value}</td>\n"; endforeach; $output .= "</tr>\n"; endforeach; This then outputs the following results table: Year | McLaren | Red Bull Racing 2016 | 79 | 175.25 2017 | 42.3 | 131.3 2018 | 57.25 | 175.95 The results for McLaren are correct but the Reb Bull Racing ones are totally out They should be (for 2016 - 2018 respectively). 96.25 89 118.7 It seems to be adding McLaren to Red Bull? Any help is much appreciated!
  4. jarvis

    Prevent Blanks in Array

    Thanks again!
  5. jarvis

    Prevent Blanks in Array

    Hi, How do you prevent the following happening: Array ( [] => Array So, effectively I want to remove that one from the main array. i.e. Array ( [] => Array ( [0] => Japanese GP [1] => Japanese GP [2] => Williams ) [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) Becomes: Array ( [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) I tried using: if (!empty($year['value'])): Within my foreach but sadly that didn't work Thanks
  6. jarvis

    Group and Total

    @Barand Once again, many thanks for your time, assistance and patience. It has been very much appreciated!!
  7. jarvis

    Group and Total

    Hi @Barand, I realised I had sort($group); which was screwing the results, removing this now shows exactly what I needed: Year: 2011 Total: 20 Year: 2012 Total: 35 Year: 2013 Total: 35 Year: 2014 Total: 45 Year: 2015 Total: 19 Year: 2016 Total: 19 Year: 2017 Total: 10 Year: 2018 Total: 12 Thank you once again for all your help
  8. jarvis

    Group and Total

    Ok thanks @Barand Just to say, using your code: foreach ($group as $year => $total) { echo "Year: $year Total: $total<br>"; } Outputs the same as my code: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 So not sure what you mean? All I'm trying to do is about the above but replacing the Year: 0 with Year 2011 (for example)
  9. jarvis

    Group and Total

    Hi @Barand, I had: foreach ($group as $k => $v) { echo "\$group[$k] => $v.\n"; } Which looks similar, but I can't get the year to show. I simply get: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 The key doesn't seem to contain the year, or once again, am I missing the obvious? I truly appreciate your help thought!
  10. jarvis

    Group and Total

    Thanks again @Barand I meant how do I loop through like so: if ($group): foreach ( $group as $g) : echo '<p>Year: '.$g['year'].' Value: '.$g['value'].'</p>'; endforeach; endif; Basically, I'm trying to build up the values so I can loop it out as: Year: 2018 Value: 2 Year: 2011 Value: 20 Year: 2012 Value: 35 Year: 2013 Value: 35 Year: 2014 Value: 10 Year: 2015 Value: 15 Year: 2016 Value: 15 Year: 2017 Value: 10 Thanks again
  11. jarvis

    Group and Total

    Hi @Barand Oh! But it produces the result I need: Array ( [2018] => 12 [2011] => 20 [2012] => 35 [2013] => 35 [2014] => 45 [2015] => 19 [2016] => 19 [2017] => 10 ) All I need is to work out how to access each one? Or have I totally missed the point? In which case, please accept my apologies!
  12. jarvis

    Group and Total

    Hi @Barand Thank you so much! Just needed to move the print_r outside the loop and it's exactly what I needed. Thanks again!
  13. jarvis

    Group and Total

    Hi All, Hoping someone can point out the blinkin obvious! I'm trying to group a bunch of data and total it. For example, the data looks like this: Year: 2018 Value: 10 Year: 2011 Value: 20 Year: 2012 Value: 35 Year: 2013 Value: 35 Year: 2014 Value: 35 Year: 2018 Value: 2 Year: 2015 Value: 4 Year: 2016 Value: 4 Year: 2014 Value: 10 Year: 2015 Value: 15 Year: 2016 Value: 15 Year: 2017 Value: 10 I then use an array to group the data by year, like so: if ($test): $group = array(); foreach ( $test as $t) : echo '<p>Year: '.$t['year'].' Value: '.$t['value'].'</p>'; $group[$t['year']][] = $t['value']; endforeach; endif; This gets me one step further by producing: Array ( [2018] => Array ( [0] => 10 [1] => 2 ) [2011] => Array ( [0] => 20 ) [2012] => Array ( [0] => 35 ) [2013] => Array ( [0] => 35 ) [2014] => Array ( [0] => 35 [1] => 10 ) [2015] => Array ( [0] => 4 [1] => 15 ) [2016] => Array ( [0] => 4 [1] => 15 ) [2017] => Array ( [0] => 10 ) ) What I really need, is to show something like: Array ( [2018] => Array ( [0] => 12 ) [2011] => Array ( [0] => 20 ) [2012] => Array ( [0] => 35 ) [2013] => Array ( [0] => 35 ) [2014] => Array ( [0] => 45 ) [2015] => Array ( [0] => 19 ) [2016] => Array ( [0] => 19 ) [2017] => Array ( [0] => 10 ) ) What would be the best way to achieve this please?
  14. jarvis

    randomise ajax for loop

    Once again, thanks cyberRobot, that worked a charm! Here's the final code in case someone else needs a hand: $.ajax({ url: '//api.instagram.com/v1/users/self/media/recent', dataType: 'jsonp', type: 'GET', data: {access_token: token, count: num_photos}, success: function(data){ data.data.sort(function() { return 0.5 - Math.random() }); for( x in data.data ){ $('#insta').append('<img src="'+data.data[x].images.low_resolution.url+'"><br/>'); } }, error: function(data){ console.log(data); } });
  15. jarvis

    randomise ajax for loop

    Hi cyberRobot, Thats correct. Glad you confirmed there are no built in functions. I'll look at that link, see if I can get that working. Many thanks
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.