Array problem

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Array problem

Post 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.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post 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];
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
Post Reply