1

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.

13
  • Hi @u_mulder A little more explanation please? What should I fix and where? Commented Apr 5, 2015 at 19:08
  • If I write it just as: foreach( $row as $k => $r ) then I get: Invalid argument supplied for foreach() Commented Apr 5, 2015 at 19:10
  • How do you obtain the $rows variable? Could you post a sample with that? Commented Apr 5, 2015 at 19:10
  • 2
    I see the problem: $rows is an object that has a 'rows' field. You need to iterate on $rows->rows in the outer form. Also with this in mind, you might change the $rows variable to something like $result. Commented Apr 5, 2015 at 19:16
  • 1
    (FYI - if you copy/paste from the page source instead of as rendered in the browser, the print_r() output will retain all linebreaks and indentation for readability) Commented Apr 5, 2015 at 19:17

1 Answer 1

2

You're iterating directly over $rows, while $rows is an object that has a $rows members that holds the actual data. So you should iterate over $rows->rows in the outer loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.