Is there a way that I can name a "variable" that I want to set, when
that "variable" might be an array element? For example:
<?php
function setMe ($varName, $varVal) {
$greeting = "Hello";
$aRay = array("there", "Fred");
$$varName = $varVal;
output( "$greeting " . join(", ", $aRay));
}
setMe ("greeting", "Goodbye"); // => Goodbye there, Fred
setMe ("aRay[1]", "Mom"); // I'd like: Hello there, Mom
?>
Thanks,
Csaba Gabor from Vienna
The actual situation is more complicated. I have a class, and the
variables in question are members of the class. Even if I do eval, I
don't get a reference to the thing that needs changing and if I can't
eval $varVal because $varVal will be an object. 2 1138
Csaba Gabor wrote: Is there a way that I can name a "variable" that I want to set, when that "variable" might be an array element?
You could try the following:
function setMe ($varName, $varVal) {
$greeting = "Hello";
$aRay = array("there", "Fred");
if (is_array($varName)) {
${$varName[0]}[$varName[1]] = $varVal;
} else {
$$varName = $varVal;
}
print( "$greeting " . join(", ", $aRay) . '<br />');
}
setMe ("greeting", "Goodbye"); // => Goodbye there, Fred
setMe (array('aRay', 1), "Mom"); // => Hello there, Mom
JW
Janwillem Borleffs wrote: Csaba Gabor wrote: Is there a way that I can name a "variable" that I want to set, when that "variable" might be an array element?
You could try the following:
function setMe ($varName, $varVal) { $greeting = "Hello"; $aRay = array("there", "Fred"); if (is_array($varName)) { ${$varName[0]}[$varName[1]] = $varVal; } else { $$varName = $varVal; } print( "$greeting " . join(", ", $aRay) . '<br />'); }
setMe ("greeting", "Goodbye"); // => Goodbye there, Fred setMe (array('aRay', 1), "Mom"); // => Hello there, Mom
Hi and thanks for your reply. I have written something like this,
except that I expect $varName as a string and then parse for
"...[number]". But the problem with that and all other solutions of
this form, including the one that you show, is that all variations must
be thought of in advance.
If, as seems the case, there is no direct method, perhaps the best tack
is to approach it recursively, but that strikes me as a fair amount of
effort for a simple effect.
Csaba Gabor from Vienna This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Denis Perelyubskiy |
last post by:
Hello,
I need to make an array of elements accross forms.
My javascript skills, as evident from this question,
are rather rudimentary.
I tried to make an associative array and index it
with...
|
by: VK |
last post by:
A while ago I proposed to update info in the group FAQ section, but I
dropped the discussion using the approach "No matter what color the cat
is as long as it still hounts the mice". Over the last...
|
by: BrianP |
last post by:
Hi,
I have had to invent a work-around to get past what looks like a
JavaScript bug, the malfunctioning Perl-like JavaScript array functions
including SPLICE() and UNSHIFT().
I have boiled it...
|
by: John Grandy |
last post by:
My ASP.NET Web Service project has a Web Method that returns an array filled
with instances of a custom class.
The custom class is defined in a Class Library that is included in the
web-service...
|
by: Newcomsas |
last post by:
Hello,
I'm trying to solve a problem with JS textbox array without success.
I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new
textbox named 'dear' is generated. So, if...
| |
by: frizzle |
last post by:
Hi there.
I have this array:
name => john, age => 45, profession => teacher
name => hank, age => 22, profession => student
name => mary, age => 36, profession => dancer
etc.
etc.
It's...
|
by: mflll |
last post by:
I am looking into the different techniques of handling arrays of edit
boxes in
Java Script. The first program below works fine. However, are there
better ways of
doing this, where the person...
|
by: Jake Barnes |
last post by:
I was just reading this article on Ajaxian:
http://ajaxian.com/archives/show-love-to-the-object-literal
This is a newbie question, but what is the object literal? I thought it
was like an...
|
by: arnuld |
last post by:
this programme gives unusual output. i am not able to find out where the
semantic bug lies:
/* C++ Primer - 4/e
*
* an example from section section 7.2.4, page 241 * STATEMENT
* write a...
|
by: memeticvirus |
last post by:
I have an array cli::array<float, 2and I would like to access a
subset of it's values by compiling an array of pointers. But, it's not
possible to create an array of type...
|
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...
| |
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...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |