473,699 Members | 2,612 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 1151
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
3182
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
6
4960
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
1804
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
6744
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
1462
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
1838
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
3448
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
1313
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
1879
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
1852
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
8705
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
8623
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
9197
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...
0
9054
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6549
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
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3071
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
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.