Jump to content

question about using while..do to iterate object arrays


pandu

Recommended Posts

Hello,

 

I am having a hard time understanding the syntax used to iterate thru an array of objects using the while..do loop. For example, in the code below, what is $row? Also, if there is documentation on this online would appreciate if you could point it to me. (Did not find anything on php.net)

public static function find_by_sql($sql="") {
	global $database;
	$result_set = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result_set)) {
		$object_array[] = self::instantiate($row);
	}
	return $object_arry;
}	

TIA

public static function find_by_sql($sql="") {

      global $database;

      $result_set = $database->query($sql);

   

      while ($row =  mysql_fetch_array($result_set) ) {

        $object_arry[] = $row['row1'];     

      }

      return $object_arry;

  } 

What you're seeing is a bit of a short form.  Think of it like this:

 

$row=mysql_fetch_array($result_set);
while($row!==false) {
  $object_arry[]=$row['row1'];
  $row=mysql_fetch_array($result_set);
}
return $object_arry;

 

because

 

while ($row =  mysql_fetch_array($result_set) ) {

 

is really parsed as

 

while (($row =  mysql_fetch_array($result_set))!==false) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.