Is it possible to put Variable in Array? Here is what I need:
$array = Array($a,$b);
$a = 'something';
$b = 'something else';
foreach ($array as $val) {
echo "$val";
}
Output should be:
something
something else
Array is:
Array
(
[0] => $a
[1] => $b
)
If this is not possible is there any other way to do this? Thing is that
I need to display some data based on array content.
So far I have used:
if (in_array('5', $array)) {
echo 'data that should be display based on array value. I cant put this
data in array because array is stored in cookie and then read';
} 9 2419
dr. zoidberg wrote: Is it possible to put Variable in Array? Here is what I need:
$array = Array($a,$b);
$a = 'something'; $b = 'something else';
foreach ($array as $val) { echo "$val"; }
Output should be:
something something else
Array is:
Array ( [0] => $a [1] => $b )
If this is not possible is there any other way to do this? Thing is that I need to display some data based on array content.
So far I have used:
if (in_array('5', $array)) {
echo 'data that should be display based on array value. I cant put this data in array because array is stored in cookie and then read';
}
Use associative arrays, i.e. use keys:
$array = array('a' => 'something',
'b' => 'something else');
Now you can have:
echo array['a'];
Berislav
--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
Berislav Lopac wrote: dr. zoidberg wrote:
Is it possible to put Variable in Array? Here is what I need:
$array = Array($a,$b);
$a = 'something'; $b = 'something else';
foreach ($array as $val) { echo "$val"; }
Output should be:
something something else
Array is:
Array ( [0] => $a [1] => $b )
If this is not possible is there any other way to do this? Thing is that I need to display some data based on array content.
So far I have used:
if (in_array('5', $array)) {
echo 'data that should be display based on array value. I cant put this data in array because array is stored in cookie and then read';
}
Use associative arrays, i.e. use keys:
$array = array('a' => 'something', 'b' => 'something else');
Now you can have:
echo array['a'];
Like I said, I will save that Array into cookie and I cant save Array
values, they would be too big for cookie.
Pedro Graca wrote: dr. zoidberg wrote: Is it possible to put Variable in Array? Here is what I need:
$array = Array($a,$b);
$a = 'something'; $b = 'something else';
foreach ($array as $val) { echo "$val"; }
Output should be:
something something else
Maybe variable variables are what you're after:
<?php $array = array('a', 'b'); $a = 'something'; $b = 'something else';
foreach ($array as $val) { echo ${$val}, "\n"; }
Even better:
$array = array('a', 'b');
$values = array('a' => 'something',
'b' => 'something else');
foreach ($array as $val) {
echo $values[$val]. "\n";
}
That way you don't infest your code with too many variables.
Berislav
--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
dr. zoidberg wrote:
<snip> Like I said, I will save that Array into cookie and I cant save Array values, they would be too big for cookie.
No, you didn't. You echoed that.
And why can you not store an array in a cookie?
If you have too much data for a cookie, the way you store it inside the
coocie won'y help you a lot.
If you have too much data for a cookie, use a session.
Regards,
Erwin Moller
Pedro Graca wrote: dr. zoidberg wrote:
Is it possible to put Variable in Array? Here is what I need:
$array = Array($a,$b);
$a = 'something'; $b = 'something else';
foreach ($array as $val) { echo "$val"; }
Output should be:
something something else
Maybe variable variables are what you're after:
<?php $array = array('a', 'b'); $a = 'something'; $b = 'something else';
foreach ($array as $val) { echo ${$val}, "\n"; } ?>
TNX!!
Berislav Lopac wrote: Pedro Graca wrote: [... inferior "solution" snipped]
Even better:
$array = array('a', 'b'); $values = array('a' => 'something', 'b' => 'something else');
foreach ($array as $val) { echo $values[$val]. "\n"; }
I wouldn't say this is even better;
I'd say this is a million times much better!
One more tip to add to bag-of-tricks:
*----------------------------------------------------------*
* Add another level of indirection and simplify your code. *
*----------------------------------------------------------*
Thank you, Berislav
--
USENET would be a better place if everybody read: : mail address : http://www.catb.org/~esr/faqs/smart-questions.html : is valid for : http://www.netmeister.org/news/learn2quote2.html : "text/plain" : http://www.expita.com/nomime.html : to 10K bytes :
Erwin Moller wrote: dr. zoidberg wrote:
<snip>
Like I said, I will save that Array into cookie and I cant save Array values, they would be too big for cookie.
No, you didn't. You echoed that. And why can you not store an array in a cookie?
If you have too much data for a cookie, the way you store it inside the coocie won'y help you a lot.
If you have too much data for a cookie, use a session.
Data needs to be retrieved againg, so session is not an option. Anyway
Pedro Grace gave me great tip, I didn't know about variable variables
before. Instead od puting while string info array (to much data), I only
put one letter per string.
Tnx all.
"dr. zoidberg" <li***@ns.cis.u7.da.ru> wrote in message
news:c4*************@ID-93631.news.uni-berlin.de... Is it possible to put Variable in Array? Here is what I need:
<snip>
On top of everything else, you can tinker with serialization - that's what
it's for.
This should work:
setcookie('myarray',serialize($myarray));
.....
$myarray=unserialize($_COOKIE['myarray']);
HTH
Garp This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Randell D. |
last post by:
I have a script (below) that can be passed an array and it will dump the
contents of the array in to an html table - I use it during development so
its nothing sexy. It handles multidimsional...
|
by: Gord |
last post by:
Hello,
I think that what I'm trying to do is impossible, but before I give up I
thought I'd try and pick a few more knowledgeable brains than my own.
I have any array of user defined type...
|
by: Dave |
last post by:
I am farly new to java and am interested in how to cover a 'range'.
ie. if variable !=
cheers,
Dave
---
Outgoing mail is certified Virus Free.
|
by: HappyHippy |
last post by:
Hi,
I'm wondering what you think about this piece of code:
#include<iostream>
int main()
{
int size;
std::cin >> size;
|
by: Friday |
last post by:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of
dot.net (and all things Windows, for that matter).
As part of an experiment (to learn enough ASP/VB.net to port a series
of ...
|
by: redefined.horizons |
last post by:
I would like to have an array declaration where the size of the array
is dependent on a variable. Something like this:
/* Store the desired size of the array in a variable named
"array_size". */...
|
by: strawberry |
last post by:
In the function below, I'd like to extend the scope of the $table
variable such that, once assigned it would become available to other
parts of the function. I thought 'global $table;' would solve...
|
by: Justcallmedrago |
last post by:
How would you declare and assign a variable inside a function THAT HAS
THE NAME OF A PARAMETER YOU PASSED
example:
when you call createvariable("myvariable")
it will declare the variable...
|
by: Al G |
last post by:
Hi,
I'm converting a bit of POP3 VB6 code to VB2005, and have run into this
error with the following code.
Can someone help me find out what I'm missing/doing wrong?
'holds the attachments...
|
by: istillshine |
last post by:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |