Re: arrays and references
| From: | Hardy Merrill | Date: | Thu, 12 Oct 2000 15:48:16 +0000 |
| Subject: | Re: arrays and references | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19827@lists.php.net to get a copy of this message | ||
Stefan Goethals [stefan@yournet.be] wrote:
>
> Hi ,
>
> pass an array by reference to your function
>
> function make_my_array( &$rows ){
>
> //steps to fill $rows with data from database...
> mysql_connect.....
> ....
> etc
So even though the function takes in a reference to an array,
inside the function you still refer to $rows as if it was an
array and not a reference to an array? Maybe I'm getting
confused because in Perl a "reference" to something is an
actual scalar "thing" - it points to something. But in PHP, it
sounds like a reference is not a tangible scalar "thing" - it
is mearly a way to make 2 different things both point to the
same thing. Am I right?
So, for an example closer to what I'm trying to do, can I do
this:
function populate_rows( &$rows ) {
$query = "SELECT id, name FROM test_table";
// connect already done - link in $db_conn
if (! $result = mysql_query($query, $db_conn)) {
// error - handle it here and exit
}
// This is the important part
while ($row = mysql_fetch_array($result)) {
array_push($rows, $row); // is this correct, or would
// I want to push a "reference"
// to $row onto array $rows?
}
}
$rows = array();
populate_rows($rows);
$row_ct = 0;
while (list($key, $value) = each ($rows)) {
$row_ct++;
echo "Row $row_ct: ID=$rows[$row_ct][id], Name=$rows[$row_ct][name]<br>";
}
>
> }
>
> us it like this,
>
> // create an empty array
> $rows = array()
>
> make_my_array( $rows );
>
> // $rows now contains your data
>
> bye
>
> --
>
> Stefan - Zipkid - Goethals
> YourNet - Open source web design & Site management.
> http://www.yournet.be
> mailto:stefan@yournet.be
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com