Retrieving Variables from Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Abolshiz
Forum Newbie
Posts: 2
Joined: Thu May 13, 2010 12:46 pm

Retrieving Variables from Arrays

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Retrieving Variables from Arrays

Post 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);
(#10850)
Abolshiz
Forum Newbie
Posts: 2
Joined: Thu May 13, 2010 12:46 pm

Re: Retrieving Variables from Arrays

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Retrieving Variables from Arrays

Post 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;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Retrieving Variables from Arrays

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply