473,903 Members | 4,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

foreach loop

424 Contributor
is it possible to have more than 1 array on a foreach loop?
Mar 18 '09 #1
19 1526
1,168 Recognized Expert Top Contributor
What do you mean? Post a sample code so we can see what you're trying to do.
Mar 18 '09 #2
25 New Member
yes, by adding array from while loop
Mar 19 '09 #3
25 New Member
Try to use while loop for getting array
Mar 19 '09 #4
424 Contributor
how would i use the while loop to add the extra array to a foreach loop?

at 1st i thought id have to to the array_combine.. ...that way i can make an extra key and value using the 2 arrays i need. But i still can't figure how to get the extra item into the foreach....once i combined my arrays i tried this

$details = array_combine($ _POST['photoT'], $_POST['captionT']);

// Loop through the POST items
foreach ( (($_POST['picT'] as $picT => $picStart)) && ($details as $photoName => $picCaption)) )


$nameCapsA = $xmlobj->addChild($picS tart);
$nameCapsA->addAttribute(" name", $photoName);
$nameCapsA->addAttribute(" caption", $picCaption);

Mar 19 '09 #5
6,050 Recognized Expert Expert
You can only use 1 array as a foreach() loop's argument.

Am I right in thinking the amount of files you upload will be the same as the amount of captions you have?

The reason I suggested you do it all in one loop, is because (if you are having the same number of items -- above) you could use the array key given from the foreach loop to access the same index of a different array.

Consider this:

Expand|Select|Wrap|Line Numbers
  1. $array_1 = array ( 1, 2, 3, 4, 5, 6 );
  2. $array_2 = array ( "one", "two", "three", "four", "five", "six" );
  4. foreach ( $array_1 as $key => $value )
  5. {
  6.     echo "{$value} = {$array_2[$key]}<br />";
  7. }
  8. // Produces:
  9. // 1 = one
  10. // 2 = two
  11. // ... etc.
Mar 19 '09 #6
424 Contributor
yes you are right in suggesting that but as i have three arrays it's not that easy......ahhhh h but?

what if i have a while loop to generate one and and the use a foreach loop within the while loop for the $key & $value...would that work better? I will do a test and post my code here
Mar 19 '09 #7
424 Contributor
also please bare in mind this is being used towards simpleXML.....i t doesn't always take php coding as it should once output.......su ch a pain
Mar 19 '09 #8
6,050 Recognized Expert Expert
Do you mean you have a multi-dimensional array, or 3 seperate arrays?

Can I please see your upload form; I can't make sense of what's happening here.
Mar 19 '09 #9
424 Contributor
the form fields are generated with php, below is what is generated. I'm trying to make an array that can be used within the foreach....i dont really know how many arrays I need but i have 3 picT, photoT, captionT.....i' ve named the fields picT[] photoT[] and captionT[] as to use them as an array when i generate the xml file

Expand|Select|Wrap|Line Numbers
  1.             echo '<td><center><p><a href="'.$img_path .'"><img src= "'.$path.$item[$n] .'" height="100" width="100"></a></p></center>';
  2.             echo '<center><p><input type="hidden" name="picT[]" value="pic'.$pn++.'"/></p></center>';
  3.             echo '<center><p><input type="hidden" name="photoT[]" value="PHOTO '.$pto++.'"/></p></center>';
  4.             echo '<center><p><input type="text" name="captionT[]" value=""/></p></center>';
  5.             echo "<center><p><a href=imgEdit.php?img=$img_path> > Edit Image < </a></p></center><br></td>";
Mar 19 '09 #10

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

Similar topics

by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of code everytime I found more than one method to perform a single task. I timed each method to find which would complete faster. I thought I'd share my most recent results which (I believe) should help those write their programs to be more...
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains on the "bit = false;" stating that bit is read-only.
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a "foreach" loop instead of a "for" loop. This made absolutely no sense to me, but the author had posted his code and timing results. The "foreach" (a VB and other languages construct) was 0.01 seconds to access 1000 lines in rich text box,...
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in findListBox.listBox2.Items) { listBox2.Items count changed, restart this foreach } Thanks for any help.
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful extension of the first: foreach (array_expression as $value) statement
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current code is here: foreach ( string propertyName in ht.Keys ) { this.setProperty( propertyName, ht );
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_content" is a...
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and matrices are sparse by design/definition in PHP. I'm doing some matrix manipulation in a Finite Element program. Translating Fortran to PHP, because hosters won't allow anything else than PHP. I wish PHP would do array and matrix stuff like Fortran...
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...
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,...
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...
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...
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...
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();...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.