473,549 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable in Array

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';

}
Jul 17 '05 #1
9 2433
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.
Jul 17 '05 #2
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.
Jul 17 '05 #3
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";
}
?>
Reference: http://www.php.net/manual/en/languag...s.variable.php

--
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 :
Jul 17 '05 #4
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.
Jul 17 '05 #5
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
Jul 17 '05 #6
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!!
Jul 17 '05 #7
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 :
Jul 17 '05 #8
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.
Jul 17 '05 #9
"dr. zoidberg" <li***@ns.cis.u 7.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('myar ray',serialize( $myarray));
.....
$myarray=unseri alize($_COOKIE['myarray']);

HTH
Garp
Jul 17 '05 #10

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

Similar topics

2
3507
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 arrays so any element that itself is an array will also be broken down and displayed... Thus... in order to keep track of tables in a multidimensional...
4
3730
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 variables. I need to loop through the array (doing certain calculations) on a particular member variable. I then want to loop through the array again...
3
2125
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.
13
21345
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;
4
2654
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 existing PHP scripts of mine to work on IIS), I have created the following simple function to compare a Website visitor's IP address against a...
8
10150
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
5
2262
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 this but it's clear that I'm misunderstanding $variable persistence. I posted a similar enquiry over at alt.php.mysql, but I guess this is a more...
13
2001
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 "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
5
3242
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 Class attachmentBlockParameter
112
5376
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 that may print some messages. foo(...) { if (!silent)
0
7521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7959
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5088
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1944
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 we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.