473,414 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

converting array $row values to $string

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

Jul 20 '06 #1
11 6652
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
Jul 20 '06 #2
Maybe this works (untested):

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

"monomaniac21" <mc******@googlemail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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

Jul 20 '06 #3
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
Jul 20 '06 #4
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:
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
Jul 20 '06 #5
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:
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
Jul 20 '06 #6
"monomaniac21" <mc******@googlemail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jul 20 '06 #7
"Hans 'pritaeas' Pollaerts" <pr******@hotmail.comwrote in message
news:e9**********@news3.zwoll1.ov.home.nl...
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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jul 20 '06 #8
monomaniac21 schrieb:
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
:-)
Jul 20 '06 #9
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
Why not just

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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 20 '06 #10
Rik
Jerry Stuckle wrote:
list($name, $address1, $address2) = mysql_fetch_array($result);
Better:
list($name, $address1, $address2) = mysql_fetch_row($result);

Grtz,
--
Rik Wasmus
Jul 20 '06 #11
Rik wrote:
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.
js*******@attglobal.net
==================
Jul 20 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adam | last post by:
Hi all, If I have a string where I know the length how do I split that into an array based on Char position. For example, split a string with a length of 100 into a 5 element Array, each of...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns...
1
by: UKuser | last post by:
Hi Guys, I have a program which converts Excel spreadsheets to Javascript and allows interactivity. However it can't convert it to PHP, which is obviously better for users to view (in case J/S...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.