views:

72

answers:

3

How can you use the following sample data either with a foreach or while -loop?

I get the following sample data by running

$answers = pg_fetch_all ( $result );.

Sample data

[0]=>
  array(3) {
    ["answer"]=>
    string(7) "This is the answer"
    ["username"]=>
    string(5) "roope"
    ["was_sent_at_time"]=>
    string(26) "2009-08-20 23:30:46.200017"
  } 
  --- cut  ---
  [3]=>
  array(3) {
    ["answer"]=>
  --- cut ----

However, I am not sure whether it is the best way to extract the data, since I am not sure how you can extract the data from the above format. I unsuccessfully ran

1

foreach ( $answers as $answers[] => $answers['answer'] ){
      echo $answers['answer']
      echo $answers['username'];
      echo $answers['was_sent_at_time'];
}

#1 is the only way by which I have got data out of the resource. I have tried the #2 way unsuccessfully.

2

    while ( $answer_row = pg_fetch_array( $result ) ) {
        echo $answer_row['username'];
        echo $answer_row['was_sent_at_time'];
        echo $answer_real['answer'];  
    }

This way gives nothing as an output which is surprising, since the sample data shows me that each answer has the corresponding fields.

+1  A: 
foreach( $answers as $quess ) {
  $an = $quess['answer'];
  $name = $quess['username'];
  $time = $quess['was_sent_at_time'];
}

You have an two dimensional array:
Array[0] holds arrays
Array[0][0] is an array of values
Array[0][1] is an array of values
......

So, the foreach loops through the first ( or container ) array. Each sub-value inside of the foreach is also an array (array[0][0], array[0][1], etc). So to access the individual values, you must access them in array style: array['index'] = value.

Robert DeBoer
What is the `quess`?
Masi
$quess is the temporary value fore each value in $answer - that is the way foreach works - for each value in x, it places a copy in temporary value y.
Robert DeBoer
+3  A: 

This is how you do the foreach loop

foreach ( $answers as $answer ){
  echo $answer['answer']
  echo $answer['username'];
  echo $answer['was_sent_at_time'];
}

and the while loop...

while ( $answer_row = pg_fetch_array( $result, NULL, PGSQL_ASSOC ) ) {
    echo $answer_row['username'];
    echo $answer_row['was_sent_at_time'];
    echo $answer_row['answer'];  
}

Note that the difference is that the foreach loop basically assigns each element in an array to another variable in order. It's kind of a wrapper around

for($i=0;$i<count($arr);$i++) {
    $value = $arr[$i];
}

And also that pg_fetch_array does not return an associative array by default. You have to pass the PGSQL_ASSOC parameter to it for it to do that.

davethegr8
+2  A: 

Since you're not interested in the key (i.e. the answer number), you can omit the first half of the second part of the foreach arguments. Here's the right way to do #1:

foreach ($answers as $answer) {
    echo $answer['answer'];
    echo $answer['username'];
    echo $answer['was_sent_at_time'];
}

pg_fetch_array is a tricky beast because it needs a third optional argument to be set to get back the data indexed by column name rather than position. Because the second argument is also optional, you have to give it a value to be able to add the third argument. The second argument is a row index, so give it an index variable:

$i = 0;
while ($answer_row = pg_fetch_array($result, $i ++, PGSQL_ASSOC)) {
    echo $answer_row['username'];
    echo $answer_row['was_sent_at_time'];
    echo $answer_row['answer'];  
}

Give that a try and see if it works.

Frank Schmitt