By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,051 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,051 IT Pros & Developers. It's quick & easy.

Variable in Array

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.