Page 1 of 1
Retrieving Variables from Arrays
Posted: Thu May 13, 2010 12:54 pm
by Abolshiz
Here's what I'm trying to do:
<?php
$array = array(1, $var);
$array[1] = 5;
echo $var;
?>
The echo statement does nothing. Instead the value 5 is displayed when I echo $array[1].
My interpretation:
$var was removed from the array and instead the value 5 was added. So $var does not exist after the third line. How can I assign $var to 5 instead.
I don't know if my interpretation is correct, but I hope anyone can help me figure this out!
Re: Retrieving Variables from Arrays
Posted: Thu May 13, 2010 2:08 pm
by Christopher
Abolshiz wrote:Here's what I'm trying to do:
<?php
$array = array(1, $var);
$array[1] = 5;
echo $var;
?>
The echo statement does nothing. Instead the value 5 is displayed when I echo $array[1].
That is because you never assign a value to $var.
Abolshiz wrote:My interpretation:
$var was removed from the array and instead the value 5 was added. So $var does not exist after the third line. How can I assign $var to 5 instead.
I don't know if my interpretation is correct, but I hope anyone can help me figure this out!
First, doing:
$array = array(1, $var);
Is the same as doing:
$array[0] = 1;
$array[1] = $var;
To set the value of $var to 5 do:
$var = 5;
Using the array() syntax, the way to do this:
$array[1] = 5;
Is to do:
$var = 5;
$array = array(1 => $var);
Re: Retrieving Variables from Arrays
Posted: Thu May 13, 2010 2:50 pm
by Abolshiz
Hey! Thnx, but I don't think you get what I mean. Maybe because my example was stupid! Here's what I really want:
I'm having trouble putting $a and $b and $c in an array called $stored_values, assigning them to values of another array called $posted_values and echoing them on the screen!
Here's what I tried to do:
<?php
$posted_values = array(4, 5, 6);
$stored_values = array($a, $b, $c);
$i = 0;
foreach($posted_values as $posted_value) {
$stored_values[$i]= $posted_value;
$i++;
}
echo $a;
echo $b;
echo $c;
?>
The three echo statements do nothing. Instead 4, 5 and 6 are echoed when I say:
<?php
echo $stored_values[0];
echo $stored_values[1];
echo $stored_values[2];
?>
I think what happened is that $a was removed and instead the value 4 was added in the first place of the array. What I want is $a to stay and to be assigned to 4 (and the same goes to $b and $c).
I don't know if my interpretation is what really happened. So I hope someone can help me figure this out!
Re: Retrieving Variables from Arrays
Posted: Thu May 13, 2010 3:25 pm
by AbraCadaver
There has to be a much better way to accomplish whatever it is you're trying to do. This looks so weird that it has to be the worst solution. Please explain more about what you want to accomplish, ignoring the current method. But for fun, this works:
Code: Select all
$a = $b = $c = '';
$posted_values = array(4, 5, 6);
$stored_values = array(&$a, &$b, &$c);
$i = 0;
foreach($posted_values as $posted_value) {
$stored_values[$i] = $posted_value;
$i++;
}
echo $a;
echo $b;
echo $c;
Re: Retrieving Variables from Arrays
Posted: Thu May 13, 2010 3:33 pm
by AbraCadaver
For completeness, what you did is this:
Code: Select all
// set 3 elements to the VALUE of 4, 5 and 6
$posted_values = array(4, 5, 6);
// set 3 elements to the VALUE of $a, $b and $c (which is nothing because they don't exist, so you get an empty string)
$stored_values = array($a, $b, $c);
$i = 0;
foreach($posted_values as $posted_value) {
//set the VALUE (which is nothing) of $stored_values[$i] to the VALUE of $posted_value
$stored_values[$i]= $posted_value;
$i++;
}
So you could've just done this:
Code: Select all
$posted_values = array(4, 5, 6);
$stored_values = $posted_values;
You can't put a variable in the array. You put the contents of the variable or the VALUE of the variable in the array. Or you can put a reference to the variable as I did. Still its probably not the best solution and may be one of the worst.