foreach

PHP foreach stack - is it possible that functions called in a for each loop are still running when the next iteration is called.

I am having problems with cURL not being able to connect to a server that returns an xml feed and am not sure if my code is stacking up and causing the problem. Is it possible the final function called in this foreach loop is still running when the next loop iteration comes round. Is it possible to make sure all functions in the loop co...

why does it loop more then once...am i missing something

There is only one record in the table so why does it loop like i have 5 table with one letter each $query = "Select * from click_tracker"; $result = mysql_query($query); $all_clicks = mysql_fetch_array($result); foreach($all_clicks as $click){ print " <table border=\"1\"> <tr> <th>Location</th> <th>Visit C...

for loop chcek previous ID and skip iteration if needed

I have this code, <?php foreach($information as $info) : ?> <option selected="no" value="<?php echo $info['grade_id'];?>" id="<?php echo $info['grade_id'];?>"> <?php echo $info['grade_desc'];?> </option> <?php endforeach; ?> Basically what this does is spit of some options for a select menu, however someti...

PHP foreach with Nested Array?

I have a nested array in which I want to display a subset of results. For example, on the array below I want to loop through all the values in nested array[1]. Array ( [0] => Array ( [0] => one [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) [1] => Array ( [...

PHP foreach invalid value

I have several like this in my HTML code: <input class="table" type="checkbox" name="interest[]" value="finger food" /> and this in my PHP code: $checkboxes = stripslashes($_POST['interest']); //process the checkboxes foreach ($checkboxes as $value) { $selectedChkbx .= $value . ", "; } I am getting: Warning: Invalid argume...

Is "for each" Microsoft specific?

Visual C++ 2010 accepts: std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); for each (auto i in v) std::cout << i << std::endl; Is this a C++0x feature or a Microsoft extension? According to Wikipedia, the syntax of C++0x's for-each loop different: int myint[] = {1,2,3,4,5}; for (int& i: myint) { std::cout <<...

PHP foreach error "Invalid argument supplied for foreach"

Hi guys, I was previously using this code and it worked perfectly fine, but when I put it on another server just now it started freaking out, I am passing a JSON object to a PHP file and traversing through it, the foreach I used seemd to work fine, but now I get "Invalid argument supplied for foreach", PHP: <?php $json = $_POST['sy...

grails + get the next value in g:each

Hi everyone, I have a list of checkboxes like this: <g:each in="${mylist}" var = "item" > <tr> <td colspan="2"><g:checkBox value="${dimension.id}" name="${item.id}"/> - ${item.name}</td> </tr> </g:each> I have to alter it so I get 2 columns in each row (2 checkboxes per row) <g:each in="${mylist}" var = "item" > <...

Iteration variable of different type than collection?

I have a collection of nullable ints. Why does compiler allows to iteration variable be of type int not int? ? List<int?> nullableInts = new List<int?>{1,2,3,null}; List<int> normalInts = new List<int>(); //Runtime exception when encounter null value //Why not compilation exception? foreach (...

Java for each, but multiple iterator types?

I have a class Polygon on which I wish to implement two iterators: one to run through all elements (vertices and edges in alternating order) just ONCE, and another to run through them ad infinitum (cyclically). From a for-each usage standpoint, my guess is that I am only going to be able to have one of the above be the default iterator ...

PHP: Recursively Process then remove DOMElements from DOMDocument

I'm using PHP's DOMDocument and related classes to work with XML. The XML contains processing instructions that must be processed in a specific order, from top to bottom, deepest to shallowest. I'm using a recursive function that takes the entire DOMDocument, then calls itself foreach child node, all the way down. I need to delete some...

PHP array keys values

I have an array like this $data = array( "some => "163", "rand" => "630", "om" => "43", "words" => "924", "as" => "4", "keys" => "54" ); How can I get each set's key associated with their values like this: foreach ($data as $stuff){ $this->$stuff["key"] = $stuff["value"]; } ...

How do I replace each item, without changing array's structure?

$array = array('lastname', 'email', 'phone'); How do I run foreach for this array and write updated value to its place? Like: foreach ($array as $item) { // do something // replace old value of this item with a new one } Example: foreach ($array as $item) { if (item == 'lastname') $item = 'firstname'; /...

How can I get the total of specific array elements?

I have a function that adds shopping cart data to an array of arrays. The array looks like this: Array ( [0] => Array ( [TypeFlag] => S [qty] => 2 [denom] => 50 [certMessage] => [totalPrice] => 100 ) [1] => Array ( [TypeFlag] => S [qty] => 1 [denom] => 25 [certMessage] => [totalPrice] => 25 ) ) What I need to do is get the total p...

Why implement IEnumerable(T) if I can just define ONE GetEnumerator?

Update: I appreciate all of the comments, which have essentially comprised unanimous opposition. While every objection raised was valid, I feel that the ultimate nail in the coffin was Ani's astute observation that, ultimately, even the one miniscule benefit that this idea ostensibly offered -- the elimination of boilerplate code -- was ...

Foreaching through grouped linq results is incredibly slow, any tips?

I did some profiling on a program that I'm running and the thing that takes the longest time is getting the results from the linq query: var Results = from a in Table group a by a.Value into b select new {Group = b}; foreach(var Result in Results) { //Do calcs } Any ideas on how I can speed this up? ...

Unique variables inside foreach()

Trying to create a variable with unique name for each $item. To prevent error "Only variables can be passed by reference". If there are 5 items in array $items, we should get 5 unique variables: $item_name_1; $item_name_2; $item_name_3; $item_name_4; $item_name_5; All of them should be empty. What is a true solution for this? ...

Looping with while and foreach in PHP

I cannot seem to get foreach to work. Maybe I do not understand it correctly. Here is my code: $statement = "SELECT * FROM categories ORDER BY name ASC"; $query = mysql_query($statement) ... ... $cals = array("sports","general","other","clubs"); foreach ($cals as $value) { /* echo "<h3>".$value."</h3>"; */ echo "<table width='100...

PHP: Is it wise to call a method&nested loop in a foreach loop?

Hi I have a method that returns an array of support tickets. Each support ticket can have many notes so I have a method that returns an array of tickets notes with that ticket id. I want to display the notes alongside the ticket which would mean nesting the get notes inside the foreach loop. foreach($tickets as $ticket){ //display...

Linq to mess up order of an array?

Update: False alarm! The source of the error was elsewhere. (See at the end of the question.) Is it possible that either Linq or foreach can mess up the order of an array? One of my testers reported to have experienced that the order of a list of items he fed in as input didn't match the order of the final list that was saved in the da...