[thelist] [PHP] oddity: popping array from function

Erik Mattheis gozz at gozz.com
Wed Feb 26 10:03:01 CST 2003


I'm getting some behavior I would consider bizarre ... but I'm sure
it's because PHP functions behave differently than I expect them to ...

I had things working fine, and decided to turn a large block of code
into a function, as I would need to use it elsewhere ... when the same
code is executed from a function, the results are different ... here's
the applicable code:

<code>
$link = mysql_connect("127.0.0.1","root","xxx");
$table = mysql_select_db("sleep");
$query = "SELECT event_type, UNIX_TIMESTAMP(common_time) AS unix_time,
common_time from events ORDER BY unix_time";
$result = mysql_query($query);

$results = array();

while ($results []= mysql_fetch_assoc($result));

$structure = array();

// this is the function, shortened to illustrate
function returnItem($begin,$end,$event_type) {
  $a = array('unix_time' => $begin,
   'event_type' => $event_type);
   return $a;
}

for ($i = 0; $i < count($results) - 1; $i++) {
		$a =
returnItem($results[$i],$results[$i+1],$results[$i]['event_type']);
		$structure []= $a;
}

print_r($structure);
</code>

This prints:
Array
(
  [0] => Array
   (
    [unix_time] => Array
     (
      [event_type] => s
      [unix_time] => 1045976400
      [common_time] => 2003-02-22 23:00:00
      )

     [event_type] => s
    )

Before I made the function, it behaved as desired, creating:

Array
(
  [0] => Array
   (
    [unix_time] =>  1045976400
    [event_type] => s
    )

If I don't fetch the common_time in the SELECT,  [common_time] =>
2003-02-22 23:00:00 is absent from the problematic results ... which
gives me a clue as to there where but not as to the why.



-----------------------
Erik Mattheis
GoZz Digital
<http://goZz.com/>
Flash and ColdFusion Development
Minneapolis, MN
-----------------------




More information about the thelist mailing list