I'm trying to parse an SQL result set and I'm running into multiple issues.
foreach ( $rows as $key => $row ) {
die(print(gettype( $row ))); // DIE 1
if ( is_array( $row ) ) {
foreach ( (array)$row as $k => $r ) {
die(print_r($r)); // DIE 2
if ( !in_array( $r['proposal'], $proposals ) ) {
array_push( $proposals, $r['proposal'] );
}
if ( !in_array( $r['question'], $scorequestions ) && $r['type'] == 'score' ) {
array_push( $scorequestions, $r['question'] );
}
if ( !in_array( $r['question'], $recommendquestions ) && $r['type'] == 'recommend' ) {
array_push( $recommendquestions, $r['question'] );
}
}
}
}
My $rows is: (https://i.sstatic.net/KWdgt.jpg)
+----------+--------+----------+-----------+----------+---------+-----------+-------------+------+
| question | title | campaign | type | proposal | avg | recommend | conditional | cnt |
+----------+--------+----------+-----------+----------+---------+-----------+-------------+------+
| 101 | Title1 | 104 | score | 38 | 6.6667 | 0 | 0 | 3 |
| 101 | Title2 | 104 | score | 39 | 9.6667 | 0 | 0 | 3 |
| 101 | Title3 | 104 | score | 40 | 8.0000 | 0 | 0 | 2 |
| 101 | Title4 | 104 | score | 41 | 3.0000 | 0 | 1 | 2 |
| 101 | Title5 | 104 | score | 42 | 9.0000 | 0 | 0 | 1 |
| 101 | Title6 | 104 | score | 43 | 9.0000 | 0 | 0 | 1 |
| 101 | Title7 | 104 | score | 44 | 7.6667 | 0 | 0 | 3 |
$rows is an SQL result set. DIE 1 print tells me $row is an array, YET the is_array() function thinks it's not an array.
If I skip that if loop (comment it out) and run the inner foreach loop, DIE 2 gives me:
Array ( [question] => 105 [campaign] => 104 [type] => recommend [proposal] => 42 [avg] => -1.0000 [recommend] => 1 [conditional] => 1 [cnt] => 1 [title] => Title5 [amount] => 300 [theme] => offline ) 1
But then I get an illegal string offset error on $r['proposal']. Not sure where I'm going wrong. I have read through multiple SO threads before posting this.
print_r()output will retain all linebreaks and indentation for readability)