foreach

Is there a more efficient way to run queries inside a loop? Memory issues.

I have a Joomla website that I've written a custom shopping cart component for. The user is basically purchasing codes we're storing in our database - these are associated with a printed incentive card. When the user checks out, I need to grab a chunk of codes from the database (however many they've purchased), then loop through the lis...

Add inputs to a complex form array using foreach loop

I'm using a foreach to get membership level information: foreach ($aMemLevels as $aMemLevel) { // Add 'mlevels' array to $aForm['inputs'] array } I have a main form variable that hold a large array: $aForm The form is setup like this: $aForm = array( 'form_attrs' => array( 'name' => 'menu_access', ...

Field assignment in a Java foreach declaration

I know that the foreach loop used in the following example doesn't compile. But does anybody know why using a field in a foreach loop declaration isn't allowed? public class Foo { private Object obj; public void run(List<Object> objects) { for (obj : objects) { process(); } } private void pr...

Finding distinct lines in large datatables

Currently we have a large DataTable (~152k rows) and are doing a for each over this to find a sub set of distinct entries (~124K rows). This is currently taking about 14 minutes to run which is just far too long. As we are stuck in .NET 2.0 as our reporting won't work with VS 2008+ I can't use linq, though I don't know if this will b...

JS: iterating over result of getElementsByClassName using Array.forEach

I want to iterate over some DOM elements, I'm doing this: document.getElementsByClassName( "myclass" ).forEach( function(element, index, array) { //do stuff }); but I get an error: document.getElementsByClassName("myclass").forEach is not a function I am using Firefox 3 so I know that both getElementsByClassName and Array.forEach a...

Enumerable.ElementAt vs foreach

I have a dictionary which I need to keep updated with incoming data, after parsing the incoming data I have to check if there are any entries in the dictionary which are not present in the incoming data (incoming data when parsed is a list and I need to map it with the dictionary entries). To avoid multiple loops to removed the entries,...

PHP: foreach and array issue

I have this: foreach($_POST as $key => $value) { $data[$key] = filter($value); } filter(); strips if any tags, and real escape them. Now I have an array in the POST form too, so I am getting errors in strip_tags() and mysql_real_escape_string. How should I let only $_POST["Searching"] not get filtered by filter(); ? ...

JSTL c:forEach, decremental number loop impossible?

I want to print decremental numbers like: <c:forEach var="i" begin="10" end="0" step="-1"> ... ${i} ... </c:forEach> then I got jsp exception: javax.servlet.jsp.JspTagException: 'step' <= 0 javax.servlet.jsp.jstl.core.LoopTagSupport.validateStep(LoopTagSupport.java:459) org.apache.taglibs.standard.tag.rt.core.ForEachTag.s...

ClassCastException in Java foreach loop

In what circumstances can ClassCastException occur in the code below: import java.util.Arrays; import java.util.List; public class Generics { static List getObjects() { return Arrays.asList(1, 2, 3); } public static void main(String[] args) { List<String> list = getObjects(); for (Object o : list) ...

Objective c "for each" (fast enumeration) -- evaluation of collection?

It seems from experimentation that the collection expression is evaluated only once. Consider this example: static NSArray *a; - (NSArray *)fcn { if (a == nil) a = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; NSLog(@"called"); return a; } ... for (NSString *s in [self fcn]) NSLog(@"%@", s); The ...

Idiomatic way to use for-each loop given an iterator?

When the enhanced for loop (foreach loop) was added to Java, it was made to work with a target of either an array or Iterable. for ( T item : /*T[] or Iterable<? extends T>*/ ) { //use item } That works great for Collection classes that only implement one type of iteration, and thus have a single iterator() method. But I find mys...

Help with foreach loop PHP

Hi. Currently producing a Wordpress plugin that allows for multiple image sliders. At the moment, to make sure that the code is valid I am having to load each sliders dynamic styling into the tags. This is fine, however it loads the styling for all the sliders, which can really start to add a lot of code to the pages source if the users...

C# Difference between Foreach and for (not performance)

Hi, Some time ago I read that foreach works with "copies" of objects and thus it can be used for information retrieval instead of its updating. I do not get it as it is entirely possible to loop through list of classes and change its field. Thanks! ...

Foreach for an array

I have an array: array(3) { [0]=> string(1) "3" [1]=> string(3) "488" [2]=> string(3) "177" } I want to have a foreach for its values, which returns the number in the array. How can I do that? ...

foreach error with codeigniter

Hey im using codeigniter and i got this error A PHP Error was encountered Severity: Notice Message: Undefined variable: query Filename: views/nyheder_view.php Line Number: 2 A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: views/nyheder_view.php Line Number: 2 my controlle...

PHP foreach help

Hi. Currently producing a Wordpress plugin that allows for multiple image sliders. At the moment, to make sure that the code is valid I am having to load each sliders dynamic styling into the tags. This is fine, however it loads the styling for all the sliders, which can really start to add a lot of code to the pages source if the users ...

foreach %dopar% + RPostgreSQL

I am using RPostgreSQL to connect to a local database. The setup works just fine on my Linux machine. R 2.11.1, Postgres 8.4. I was playing with the 'foreach' with the multicore (doMC) parallel backend to wrap some repetitive queries (numbering a few thousand) and appending the results into a data structure. Curiously enough, it works i...

MySQL and PHP - For Each?

Hi all, I have following code: SELECT q21, q21coding AS Description FROM `tresults_acme` WHERE q21 IS NOT NULL AND q21 <> '' ORDER BY q21coding It brings back the following (excerpt): Text Description Lack of up to date equal pay cases&legislation - t... Content needs updating The...

PHP problem with DOM parsing

Hi all. The code pasted below, works on my PC, but not on my hosting (which have PHP 5.2.13 installed). $source = file_get_contents('http://example.com/example', 0); $dom = new DOMDocument; @$dom->loadHTML($source); $dom->preserveWhiteSpace = false; $xpath = new DOMXPath($dom); $tags = $xpath->query('//div[@class="item"]'); $xml = '<...

If the supplied array of a foreach loop is a function call, is there a performance hit?

Possible Duplicate: PHP: how is an array in a foreach loop read? If I have a foreach loop that looks like this: //$test->result() returns an array foreach ($test->result() as $index=>$value) ... Is there an appreciable performance difference as opposed to storing that result in a variable and then using that as the supplied...