473,749 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a very simple questions about array

15 New Member
for example, we can make an array simply by the below method
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $var1 = a;
  3. $var2 = b;
  4. $var3 = c;
  5. $variables = array($var1,$var2,$var3);
  6. ?>
  7.  
Below, we can use "for loop" to echo out 0,1,2,3,4....20 ,
However, how can we use the "echo-out(0,1,2,3,4.. ..)" to make an array
like the above one???
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. for ($i=0; $i< 20; $i = $i + 1){ echo $i.",";}
  3. ?>
  4.  
Nov 7 '07 #1
3 1153
r035198x
13,262 MVP
for example, we can make an array simply by the below method
<?php
$var1 = a;
$var2 = b;
$var3 = c;
$variables = array($var1,$va r2,$var3);
?>


Below, we can use "for loop" to echo out 0,1,2,3,4....20 ,
However, how can we use the "echo-out(0,1,2,3,4.. ..)" to make an array
like the above one???
<?php
for ($i=0; $i< 20; $i = $i + 1){ echo $i.",";}
?>
1.) Use code tags when posting code
2.) Always refuse to write a single line of code if you don't have the manual pages with you.
Nov 7 '07 #2
adamalton
93 New Member
I'm not sure that I entirely understand what you're trying to do, but I think you might be after this:

Make an array[PHP]$my_array=array ('cheese','carr ots','peanuts') ;[/PHP]
Then print each item:[PHP]$number_of_item s=count($my_arr ay);
for($i=0; $i<$number_of_i tems; $i++)
{
echo $my_array[$i];
}[/PHP]

You could just do:[PHP]foreach($my_arr ay as $item)
{
echo $item;
}[/PHP]
It saves having to count() the number of elements first.
I hope that helps
Nov 7 '07 #3
pbmods
5,821 Recognized Expert Expert
To add to Adam's post:

for is never the appropriate choice when you want to iterate through each element in an array; that's exactly why foreach was invented.

With a foreach loop, each element gets assigned to a variable. In a for loop, however, PHP has to look up two variables and an array index. Very slow.

Get in the habit of using foreach to iterate through arrays, and you will notice a dramatic speed increase (I've seen applications go from 1.5 seconds per page load down to 0.03 seconds per page load, and 0.8 seconds worth of that was from switching to foreach loops).

(if you were curious, the remaining 0.67 seconds was saved by optimizing MySQL queries)
Nov 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3184
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
6
4963
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help...
18
1809
by: Geoff Cox | last post by:
Hello, I am trying to print out the array values for a second time but get error on page message? Thanks Geoff <html>
7
6745
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ Where S is the command for SEND and should just be the character S. Where W is a byte representing how long the filename (testfile.txt) is. In this case 12. Where XXXXXXX is converted from a string that...
1
1464
by: number1.email | last post by:
Hello, I have a simple Web Page Questionairre in which questions are read from a database, and the user can indicate the correct answer via either a radio input control or a dropdown list. The number of questions that is displayed on the screen can vary...depending on the number of questions that satisfy certain criteria. Does anyone have any sample code, or can show me how I can validate this Web Page in JavaScript so that the user is...
27
1845
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your task is to find two
24
3455
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
12
1320
by: Gilbert | last post by:
H, i'm starting with asp.net/vb.net and have some questions about arrays and collections: 1) what's the difference between: dim x() as string and dim x as array
4
1882
by: sam | last post by:
hI, I am little confused here See i have int wordlen=10; when int s is array s++; whats the meaning of this
3
1856
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array index through a out parameter. The issue is that the out parameter is not being updated. If I return the position to the caller (instead to use a out parameter) all is ok.
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.