Page 1 of 1

Array problem

Posted: Mon Mar 20, 2006 5:20 am
by spacebiscuit
Hi,

I am having a problem with an array, take a look at the code below:

Code: Select all

$variable=123456;

$variable[0]=$variable;

echo"$variable";
As you can see I am trying to write the contents of a variable into an array with the same name as the original variable. I have also tried using a temp variable as a buffer but it does not work. When I try to echo the array I just get the value 1. Do I need to some how empty or flush the variable name before I can achieve my aim?

Thanks,

Rob.

Posted: Mon Mar 20, 2006 5:41 am
by duk
well if $variable is a array, and you want to ouput the content, you need to tell what position in the array is located the content

like echo $variable[0];

Posted: Mon Mar 20, 2006 6:13 am
by spacebiscuit
Opps osrry I am already doing that, I missed the index out on my example.

When I do this..........

Code: Select all

echo"$variable[0]";
It outputs only '1'.

Rob.

Posted: Mon Mar 20, 2006 7:58 am
by spacebiscuit
The error message I receive is......

Cannot use a scalar value as an array

So I guess I somewhow need to flush the variable so that it can be re-intiatiated, is this possible?

Rob.

Posted: Mon Mar 20, 2006 8:28 am
by spacebiscuit
It seems that this does the trick...

Code: Select all

$variable = Array();
So my code becomes.....

Code: Select all

$variable=123456; 

$temp=$variable

$variable = Array(); 

$variable[0]=$temp; 

echo"$variable[0]";
Thanks,

Rob.