niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » PHP
converting array values to
Post new topic   Reply to topic Page 1 of 1 [12 Posts] View previous topic :: View next topic
Author Message
monomaniac21
*nix forums addict


Joined: 03 Sep 2005
Posts: 70

PostPosted: Thu Jul 20, 2006 10:17 am    Post subject: converting array $row values to $string Reply with quote

hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc
Back to top
Hans 'pritaeas' Pollaerts
*nix forums beginner


Joined: 18 Jul 2006
Posts: 3

PostPosted: Thu Jul 20, 2006 10:38 am    Post subject: Re: converting array $row values to $string Reply with quote

Maybe this works (untested):

$row = mysql_fetch_array($result);
foreach ($row as $name => $value) {
$eval = "${$name} = $value";
eval ($eval);
}

"monomaniac21" <mcyi2mr3@googlemail.com> wrote in message
news:1153390678.605323.145130@h48g2000cwc.googlegroups.com...
Quote:
hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc
Back to top
Nyoka
*nix forums beginner


Joined: 20 Jul 2006
Posts: 2

PostPosted: Thu Jul 20, 2006 10:42 am    Post subject: Re: converting array $row values to $string Reply with quote

You could write your own function similar to:
function convertArray ($array) {
foreach ($array AS $name => $value) {
global $$name;
$$name = $value;
}
}

Make sure you do a print_r ($array) before using this so you are away
of what variables are going to be used.

monomaniac21 wrote:
Quote:
hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc
Back to top
monomaniac21
*nix forums addict


Joined: 03 Sep 2005
Posts: 70

PostPosted: Thu Jul 20, 2006 10:45 am    Post subject: Re: converting array $row values to $string Reply with quote

Cheers Nyoka I'm sure it does work.

but i dont understand it. what is the double $$ for and "global"?

:-)


Nyoka wrote:
Quote:
You could write your own function similar to:
function convertArray ($array) {
foreach ($array AS $name => $value) {
global $$name;
$$name = $value;
}
}

Make sure you do a print_r ($array) before using this so you are away
of what variables are going to be used.

monomaniac21 wrote:
hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc
Back to top
monomaniac21
*nix forums addict


Joined: 03 Sep 2005
Posts: 70

PostPosted: Thu Jul 20, 2006 11:03 am    Post subject: Re: converting array $row values to $string Reply with quote

What i cant see is how the function finds out the name given to each
row of the array?

Can anyone explain this to me?

monomaniac21 wrote:

Quote:
Cheers Nyoka I'm sure it does work.

but i dont understand it. what is the double $$ for and "global"?

:-)


Nyoka wrote:
You could write your own function similar to:
function convertArray ($array) {
foreach ($array AS $name => $value) {
global $$name;
$$name = $value;
}
}

Make sure you do a print_r ($array) before using this so you are away
of what variables are going to be used.

monomaniac21 wrote:
hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc
Back to top
ImOk
*nix forums beginner


Joined: 07 Jul 2006
Posts: 29

PostPosted: Thu Jul 20, 2006 11:25 am    Post subject: Re: converting array $row values to $string Reply with quote

After you do the fetch run

extract($row);

This will cretae memory variables of the same name as the keys and
containing the values.

Study the example from docs:

$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");

echo "$color, $size, $shape, $wddx_size\n";




monomaniac21 wrote:
Quote:
hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc
Back to top
Kimmo Laine
*nix forums Guru Wannabe


Joined: 25 Mar 2005
Posts: 209

PostPosted: Thu Jul 20, 2006 11:34 am    Post subject: Re: converting array $row values to $string Reply with quote

"monomaniac21" <mcyi2mr3@googlemail.com> wrote in message
news:1153393402.467503.199960@i42g2000cwa.googlegroups.com...
Quote:
What i cant see is how the function finds out the name given to each
row of the array?

Can anyone explain this to me?


It's using a feature of php called variable variable.

let's say you have a couple of variables:
$name = "fred flintstone";
$pointer = "name";

You can point to variable $name with the $pointer using the variable
variable syntax: $$pointer is evaluated as $"name", and since $pointer
contains the string "name".

echo $$pointer; // This will print "fred flintstone"

The very same mechanisms is used in the example function.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)
Back to top
Kimmo Laine
*nix forums Guru Wannabe


Joined: 25 Mar 2005
Posts: 209

PostPosted: Thu Jul 20, 2006 11:37 am    Post subject: Re: converting array $row values to $string Reply with quote

"Hans 'pritaeas' Pollaerts" <pritaeas@hotmail.com> wrote in message
news:e9nmo0$6un$1@news3.zwoll1.ov.home.nl...
Quote:
Maybe this works (untested):

$row = mysql_fetch_array($result);
foreach ($row as $name => $value) {
$eval = "${$name} = $value";
eval ($eval);
}


No reason to get eval involved.
This:

foreach($row as $name => $value)
$$name = $value;

works just fine.


--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)
Back to top
Markus Ernst
*nix forums addict


Joined: 22 Feb 2005
Posts: 61

PostPosted: Thu Jul 20, 2006 11:41 am    Post subject: Re: converting array $row values to $string Reply with quote

monomaniac21 schrieb:
Quote:
Cheers Nyoka I'm sure it does work.

but i dont understand it. what is the double $$ for and "global"?

manual_exists() == true!

www.php.net/global
www.php.net/variables
Smile
Back to top
Jerry Stuckle
*nix forums Guru


Joined: 24 Feb 2005
Posts: 1515

PostPosted: Thu Jul 20, 2006 12:33 pm    Post subject: Re: converting array $row values to $string Reply with quote

monomaniac21 wrote:
Quote:
hi all

is there a function to convert all values of array $row into seperate
variables:

$row = mysql_fetch_array($result);

// i dont want to have to do this anymore :-)

$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc




Regards

marc


Why not just

list($name, $address1, $address2) = mysql_fetch_array($result);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Back to top
Rik
*nix forums Guru Wannabe


Joined: 16 Nov 2005
Posts: 291

PostPosted: Thu Jul 20, 2006 2:12 pm    Post subject: Re: converting array $row values to $string Reply with quote

Jerry Stuckle wrote:
Quote:
list($name, $address1, $address2) = mysql_fetch_array($result);

Better:
list($name, $address1, $address2) = mysql_fetch_row($result);

Grtz,
--
Rik Wasmus
Back to top
Jerry Stuckle
*nix forums Guru


Joined: 24 Feb 2005
Posts: 1515

PostPosted: Thu Jul 20, 2006 4:59 pm    Post subject: Re: converting array $row values to $string Reply with quote

Rik wrote:
Quote:
Jerry Stuckle wrote:

list($name, $address1, $address2) = mysql_fetch_array($result);


Better:
list($name, $address1, $address2) = mysql_fetch_row($result);

Grtz,

True.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [12 Posts] View previous topic :: View next topic
The time now is Mon Dec 01, 2008 8:42 pm | All times are GMT
navigation Forum index » Programming » PHP
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts FAQ 4.32 How do I strip blank space from the beginning/en... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 pm
No new posts FAQ 4.34 How do I extract selected columns from a string? PerlFAQ Server Perl 0 Fri Jul 21, 2006 7:03 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am
No new posts determine pointer to point to array or single item during... yancheng.cheok@gmail.com C++ 5 Fri Jul 21, 2006 1:17 am
No new posts FAQ 4.41 How can I remove duplicate elements from a list ... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 am

Bad Credit Mortgages | Gundam 00 | Работа в Канаде | Loans | Credit Cards
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2399s ][ Queries: 16 (0.1290s) ][ GZIP on - Debug on ]