We help IT Professionals succeed at work.

Parsing JSON results not going as planned

Eddie Shipman
on
Been up for 18+ hours getting a script ready for a non-profit that I do pro-bono work for and having difficulty seeing the forest for the trees. ;-)
I have this JSON being returned from my DB:
    [{"sponsorship1":"1","sponsorship2":"1","sponsorship3":"1","sponsorship4":"0","sponsorship5":"1","sponsorship6":"0","sponsorship7":"1","sponsorship8":"1","sponsorship9":"1","sponsorship10":"1","sponsorship11":"1","sponsorship12":"1"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"1","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"1","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"1","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"},
     {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}
    ]

Open in new window

I need to set these variables based on all the records in the table:
    $s1_active = false;
    $s2_active = false;
    $s3_active = false;
    $s4_active = false;
    $s5_active = false;
    $s6_active = false;
    $s7_active = false;
    $s8_active = false;
    $s9_active = false;
    $s10_active = false;
    $s11_active = false;
    $s12_active = false;

Open in new window

If any sponsorship1 value is 1, then $s1_active needs to be set to True (or 1).
So this code:
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$parsed = json_decode($result, TRUE);

if (curl_error($ch)) {
    trigger_error(curl_error($ch)); 
}


//close connection
curl_close($ch);

// set default to false
$s1_active = false;
$s2_active = false;
$s3_active = false;
$s4_active = false;
$s5_active = false;
$s6_active = false;
$s7_active = false;
$s8_active = false;
$s9_active = false;
$s10_active = false;
$s11_active = false;
$s12_active = false;

foreach($parsed as $record) {
    for($i = 1; $i<13; $i++) 
    {
        switch($i)
        {
            case 1:
                $s1_active = $record['sponsorship'.$i] === 1;
                break;
            case 2:
                $s2_active = $record['sponsorship'.$i] === 1;
                break;
            case 3:
                $s3_active = $record['sponsorship'.$i] === 1;
                break;
            case 4:
                $s4_active = $record['sponsorship'.$i] === 1;
                break;
            case 5:
                $s5_active = $record['sponsorship'.$i] === 1;
                break;
            case 6:
                $s6_active = $record['sponsorship'.$i] === 1;
                break;
            case 7:
                $s7_active = $record['sponsorship'.$i] === 1;
                break;
            case 8:
                $s8_active = $record['sponsorship'.$i] === 1;
                break;
            case 9:
                $s9_active = $record['sponsorship'.$i] === 1;
                break;
            case 10:
                $s10_active = $record['sponsorship'.$i] === 1;
                break;
            case 11:
                $s11_active = $record['sponsorship'.$i] === 1;
                break;
            case 12:
                $s12_active = $record['sponsorship'.$i] === 1;
                break;
        }
    }
}
echo "1:".($s1_active?"True":"False")."\n";
echo "2:".($s2_active?"True":"False")."\n";
echo "3:".($s3_active?"True":"False")."\n";
echo "4:".($s4_active?"True":"False")."\n";
echo "5:".($s5_active?"True":"False")."\n";
echo "6:".($s6_active?"True":"False")."\n";
echo "7:".($s7_active?"True":"False")."\n";
echo "8:".($s8_active?"True":"False")."\n";
echo "9:".($s9_active?"True":"False")."\n";
echo "10:".($s10_active?"True":"False")."\n";
echo "11:".($s11_active?"True":"False")."\n";
echo "12:".($s12_active?"True":"False")."\n";

Open in new window

should produce this result:
    1:True
    2:True
    3:True
    4:False
    5:True
    6:False
    7:True
    8:True
    9:True
    10:True
    11:True
    12:True

Open in new window


But I'm getting all of them set to False. I guess it must be overwriting on each record?
Comment
Watch Question

Ryan ChongSoftware Team Lead

Commented:
I guess you forgot to mention the important part > how you load the JSON in php into variables $s1_active to ... $s10_active ?

just thinking if an array (of class object) is better to handle this scenario?
Eddie ShipmanAll-around developer

Author

Commented:
Question updated with all code.
Michel PlungjanIT Expert
Top Expert 2009

Commented:

If you can use JavaScript then this would work


https://jsfiddle.net/mplungjan/9xLp3t6d/


const arr = [{"sponsorship1":"1","sponsorship2":"1","sponsorship3":"1","sponsorship4":"0","sponsorship5":"1","sponsorship6":"0","sponsorship7":"1","sponsorship8":"1","sponsorship9":"1","sponsorship10":"1","sponsorship11":"1","sponsorship12":"1"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"1","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"1","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"1"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"1","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}, {"sponsorship1":"0","sponsorship2":"0","sponsorship3":"0","sponsorship4":"0","sponsorship5":"0","sponsorship6":"0","sponsorship7":"0","sponsorship8":"0","sponsorship9":"0","sponsorship10":"0","sponsorship11":"0","sponsorship12":"0"}] 
 
const sums = arr.reduce((acc, curr) => { 
  Object.keys(curr).forEach(key => { if (!acc[key]) acc[key] = +curr[key]; else  acc[key] += +curr[key] }) 
  return acc; 
}, {}) 
for (let [key,val] of Object.entries(sums)) console.log(key,val > 0);
NorieAnalyst Assistant

Commented:
Eddie

Not sure how you get the result you posted based on the original data and I think Ryan has a good point about using an array but what you could do with the existing code is introduce an Or (||).

You might also want to consider using '==' instead of '===' in case the '1' and '0' in the JSON are being returned as text rather than numbers.
        switch($i)
        {
            case 1:
                $s1_active = $s1_active || $record['sponsorship'.$i] == 1;
                break;
            case 2:
                $s2_active = $s2_active ||  $record['sponsorship'.$i] == 1;
                break;

        // etc

Open in new window


PS This is the code I got to work with the data from your original post in simple text file.
$result = file_get_contents("data.json");
$parsed = json_decode($result, TRUE);

$s1_active = false;
$s2_active = false;
$s3_active = false;
$s4_active = false;
$s5_active = false;
$s6_active = false;
$s7_active = false;
$s8_active = false;
$s9_active = false;
$s10_active = false;
$s11_active = false;
$s12_active = false;

foreach($parsed as $record) {

    for($i = 1; $i<13; $i++) 
    {
        switch($i)
        {
            case 1:
                $s1_active = $s1_active || $record['sponsorship'.$i] ==1;
                break;
            case 2:
                $s2_active =$s2_active || $record['sponsorship'.$i] == 1;
                break;
            case 3:
                $s3_active =$s3_active || $record['sponsorship'.$i] == 1;
                break;
            case 4:
                $s4_active =$s4_active || $record['sponsorship'.$i] == 1;
                break;
            case 5:
                $s5_active =$s5_active || $record['sponsorship'.$i] == 1;
                break;
            case 6:
                $s6_active =$s6_active || $record['sponsorship'.$i] == 1;
                break;
            case 7:
                $s7_active =$s7_active || $record['sponsorship'.$i] == 1;
                break;
            case 8:
                $s8_active =$s8_active || $record['sponsorship'.$i] == 1;
                break;
            case 9:
                $s9_active =$s9_active || $record['sponsorship'.$i] == 1;
                break;
            case 10:
                $s10_active =$s10_active || $record['sponsorship'.$i] == 1;
                break;
            case 11:
                $s11_active =$s11_active || $record['sponsorship'.$i] == 1;
                break;
            case 12:
                $s12_active =$s12_active || $record['sponsorship'.$i] == 1;
                break;
        }
    }
}
echo "1:".($s1_active?"True":"False")."\n";
echo "2:".($s2_active?"True":"False")."\n";
echo "3:".($s3_active?"True":"False")."\n";
echo "4:".($s4_active?"True":"False")."\n";
echo "5:".($s5_active?"True":"False")."\n";
echo "6:".($s6_active?"True":"False")."\n";
echo "7:".($s7_active?"True":"False")."\n";
echo "8:".($s8_active?"True":"False")."\n";
echo "9:".($s9_active?"True":"False")."\n";
echo "10:".($s10_active?"True":"False")."\n";
echo "11:".($s11_active?"True":"False")."\n";
echo "12:".($s12_active?"True":"False")."\n";

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
18 hours will do this to you.

Here is what you are doing

1. You initialize some variables to defaults
2. You iterate over an array of records with different values - but which is largely irrelevant because only the last record matters
Why?
Because you don't include variable initialization in the foreach loop
This
$s1_active = false;
$s2_active = false;
$s3_active = false;
$s4_active = false;
$s5_active = false;
$s6_active = false;
$s7_active = false;
$s8_active = false;
$s9_active = false;
$s10_active = false;
$s11_active = false;
$s12_active = false;

Open in new window

Happens OUTSIDE the foreach.
So all that is happening is you are contributing to climate change by spinning the CPU's wheels and setting your variables to the values of the last record - which happen to be all 0's - giving you the false values that you are seeing.

One  solution is as Norie has outlined - the other is simply to add the sponsorship values on each iteration.
Any value > 0 is the same as a 1 and can be reduced to a falsy / truthy value very easily - see below
$s1_active = 0;
$s2_active = 0;
$s3_active = 0;
$s4_active = 0;
$s5_active = 0;
$s6_active = 0;
$s7_active = 0;
$s8_active = 0;
$s9_active = 0;
$s10_active = 0;
$s11_active = 0;
$s12_active = 0;

foreach($parsed as $rec) {
  // CAST OBJECT TO ARRAY WE CAN WORK WITH IT AS AN ARRAY
  $record = (array)$rec;
  for($i = 1; $i<13; $i++) 
  {
    switch($i)
    {
      case 1:
        $s1_active += $record['sponsorship'.$i];
        break;
      case 2:
        $s2_active += $record['sponsorship'.$i];
        break;
      case 3:
        $s3_active += $record['sponsorship'.$i];
        break;
      case 4:
        $s4_active += $record['sponsorship'.$i];
        break;
      case 5:
        $s5_active += $record['sponsorship'.$i];
        break;
      case 6:
        $s6_active += $record['sponsorship'.$i];
        break;
      case 7:
        $s7_active += $record['sponsorship'.$i];
        break;
      case 8:
        $s8_active += $record['sponsorship'.$i];
        break;
      case 9:
        $s9_active += $record['sponsorship'.$i];
        break;
      case 10:
        $s10_active += $record['sponsorship'.$i];
        break;
      case 11:
        $s11_active += $record['sponsorship'.$i];
        break;
      case 12:
        $s12_active += $record['sponsorship'.$i];
        break;
    }
  }
}
echo "1:".($s1_active?"True":"False")."\n";
echo "2:".($s2_active?"True":"False")."\n";
echo "3:".($s3_active?"True":"False")."\n";
echo "4:".($s4_active?"True":"False")."\n";
echo "5:".($s5_active?"True":"False")."\n";
echo "6:".($s6_active?"True":"False")."\n";
echo "7:".($s7_active?"True":"False")."\n";
echo "8:".($s8_active?"True":"False")."\n";
echo "9:".($s9_active?"True":"False")."\n";
echo "10:".($s10_active?"True":"False")."\n";
echo "11:".($s11_active?"True":"False")."\n";
echo "12:".($s12_active?"True":"False")."\n";

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
If we really want to be cute and make use of PHP's full functionality we can also just do this
$s1_active = 0;
$s2_active = 0;
$s3_active = 0;
$s4_active = 0;
$s5_active = 0;
$s6_active = 0;
$s7_active = 0;
$s8_active = 0;
$s9_active = 0;
$s10_active = 0;
$s11_active = 0;
$s12_active = 0;

foreach($parsed as $rec) {
    for($i = 1; $i<13; $i++) 
    {
		$src = "s{$i}_active";
		$trg = "sponsorship{$i}";
		$$src += $rec->{$trg};
    }
}
echo "1:".($s1_active?"True":"False")."\n";
echo "2:".($s2_active?"True":"False")."\n";
echo "3:".($s3_active?"True":"False")."\n";
echo "4:".($s4_active?"True":"False")."\n";
echo "5:".($s5_active?"True":"False")."\n";
echo "6:".($s6_active?"True":"False")."\n";
echo "7:".($s7_active?"True":"False")."\n";
echo "8:".($s8_active?"True":"False")."\n";
echo "9:".($s9_active?"True":"False")."\n";
echo "10:".($s10_active?"True":"False")."\n";
echo "11:".($s11_active?"True":"False")."\n";
echo "12:".($s12_active?"True":"False")."\n";

Open in new window

Eddie ShipmanAll-around developer

Author

Commented:
Thanks guys, sometimes you just can't get your head wrapped around a problem and EE always comes through!