Variable Names

This is a discussion on Variable Names within the PHP General forums, part of the PHP Programming Forums category; Hi, I seem to be having a problem in assigning a value to an array where the array is called ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-29-2007
Shaun
 
Posts: n/a
Default Variable Names

Hi,

I seem to be having a problem in assigning a value to an array where the
array is called dynamically.

e.g. the physical name for the array is "my_array", so:
my_array[1] = "test";
works fine.

$array_name = "my_array";
$array_name[1] = "test";
does not work.

I have tried $$array_name[1] = "test"; but to no avail.

Here is my code

/**
* Adds any posted values from the application form to the session
*/
function add_form_to_session() {
foreach ($_POST AS $key => $value) {
if (is_array($value)) {
foreach ($value AS $subkey => $subvalue) {
$_SESSION['ses-app-form']->$key[$subkey] = $subvalue;
//echo "value: ".$key."<br>";
}
}

else {
$_SESSION['ses-app-form']->$key = $value;
//echo "value: ".$key." = ".$value."<br>";
}
}
}


Any Ideas?

Cheers,

Shaun
Reply With Quote
  #2 (permalink)  
Old 11-29-2007
news_yodpeirs@thoftware.de
 
Posts: n/a
Default Re: Variable Names

The manual says:

In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs to
know if you meant to use $a[1] as a variable, or if you wanted $$a as the
variable and then the [1] index from that variable. The syntax for resolving
this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Does this help you?

Thomas
Reply With Quote
  #3 (permalink)  
Old 11-29-2007
Frank Ruske
 
Posts: n/a
Default AW: [PHP] Variable Names

<?php
$foo = "my_array";
$$foo = array(0 => "bar");

var_dump($my_array);
?>
array(1) { [0]=> string(3) "bar" }


Regards Frank

-----Ursprüngliche Nachricht-----
Von: Shaun [mailto:shaunthornburgh@hotmail.com]
Gesendet: Donnerstag, 29. November 2007 13:57
An: php-general@lists.php.net
Betreff: [php] Variable Names


Hi,

I seem to be having a problem in assigning a value to an array where the
array is called dynamically.

e.g. the physical name for the array is "my_array", so:
my_array[1] = "test";
works fine.

$array_name = "my_array";
$array_name[1] = "test";
does not work.

I have tried $$array_name[1] = "test"; but to no avail.

Here is my code

/**
* Adds any posted values from the application form to the session
*/
function add_form_to_session() {
foreach ($_POST AS $key => $value) {
if (is_array($value)) {
foreach ($value AS $subkey => $subvalue) {
$_SESSION['ses-app-form']->$key[$subkey] = $subvalue;
//echo "value: ".$key."<br>";
}
}

else {
$_SESSION['ses-app-form']->$key = $value;
//echo "value: ".$key." = ".$value."<br>";
}
}
}


Any Ideas?

Cheers,

Shaun

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Reply With Quote
  #4 (permalink)  
Old 11-29-2007
Frank Ruske
 
Posts: n/a
Default AW: [PHP] Variable Names

<?php
$foo = "my_array";
$$foo = array(0 => "bar");

var_dump($my_array);
?>
array(1) { [0]=> string(3) "bar" }


Regards Frank

-----Ursprüngliche Nachricht-----
Von: Shaun [mailto:shaunthornburgh@hotmail.com]
Gesendet: Donnerstag, 29. November 2007 13:57
An: php-general@lists.php.net
Betreff: [php] Variable Names


Hi,

I seem to be having a problem in assigning a value to an array where the
array is called dynamically.

e.g. the physical name for the array is "my_array", so:
my_array[1] = "test";
works fine.

$array_name = "my_array";
$array_name[1] = "test";
does not work.

I have tried $$array_name[1] = "test"; but to no avail.

Here is my code

/**
* Adds any posted values from the application form to the session
*/
function add_form_to_session() {
foreach ($_POST AS $key => $value) {
if (is_array($value)) {
foreach ($value AS $subkey => $subvalue) {
$_SESSION['ses-app-form']->$key[$subkey] = $subvalue;
//echo "value: ".$key."<br>";
}
}

else {
$_SESSION['ses-app-form']->$key = $value;
//echo "value: ".$key." = ".$value."<br>";
}
}
}


Any Ideas?

Cheers,

Shaun

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:03 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0