473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

adding items to an array

I have a for-loop to add items to an array, but when i use a for loop
to print these items, iI only get the last item of the array. This is
a simplified example of what i do, what is wrong with it:

for ($i=0;$i<10;$i++)
{
$test=array($i=>$i);
}

for ($t=0;$t<10;$t++)
{
echo "array".$t."= ".$test[$t]."<BR>";
}

Thanks in advance,

Kees
Jul 17 '05 #1
2 25359
Kees S. <in*********@hccnet.nl> wrote:
I have a for-loop to add items to an array, but when i use a for loop
to print these items, iI only get the last item of the array. This is
a simplified example of what i do, what is wrong with it:

for ($i=0;$i<10;$i++)
{
$test=array($i=>$i);
}

for ($t=0;$t<10;$t++)
{
echo "array".$t."= ".$test[$t]."<BR>";
}

Because you create (and subsequently overwrite) an array in each
iteration.

$test=array();

for ($i=0;$i<10;$i++)
{
$test[] = $i;
}

for ($t=0;$t<10;$t++)
{
echo "array".$t."= ".$test[$t]."<BR>";
}

HTH;
JOn
Jul 17 '05 #2
Jon,

It works!

Thx for your explanation, i searched for hours to solve this problem
and it is just that easy :)

Kind regards,
Kees

Because you create (and subsequently overwrite) an array in each
iteration.

$test=array();

for ($i=0;$i<10;$i++)
{
$test[] = $i;
}

for ($t=0;$t<10;$t++)
{
echo "array".$t."= ".$test[$t]."<BR>";
}

HTH;
JOn


Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: R.G. Vervoort | last post by:
Not sure if it is a php question but since i am working in php here it is. Ho can i add items to (the item label=visual text and the value=index number) a list object on a website. I am having...
0
by: Stephen | last post by:
I have been getting on well with help from this forum trying to create an array list and work with it. Everything is working fine apart from displaying my array list items into the labels in my...
1
by: Brian Henry | last post by:
I haven't worked with java script much, I know how to do this server side by constant post backs to the server on a webform, but it seems like i should be able to do this client side also... ...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
5
by: Extremest | last post by:
Is there a way to add new items to an arraylist witht he indexer? like with a normal array I can just do like array = new. What I have read with arraylist it will throw an exception if I try to...
2
by: =?Utf-8?B?TWFobW91ZCBTaGFiYW4=?= | last post by:
i have a problem in adding new listbox items i don't need to allow adding multible items with the same textvalue ex: if current items are: green red
9
by: Kiamari | last post by:
here's what i have to do: Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a...
1
by: Cainnech | last post by:
Hi, I'm working on a script and for debugging I want to check what values are in my array. Now instead of putting alerts everywhere in the code I wanted to put an inputbox with an alertbutton....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.