Does C have a “foreach” loop construct?

C doesn’t have a foreach, but macros are frequently used to emulate that: #define for_each_item(item, list) \ for(T * item = list->head; item != NULL; item = item->next) And can be used like for_each_item(i, processes) { i->wakeup(); } Iteration over an array is also possible: #define foreach(item, array) \ for(int keep = 1, \ count … Read more

JavaScript foreach loop on an associative array object

The .length property only tracks properties with numeric indexes (keys). You’re using strings for keys. You can do this: var arr_jq_TabContents = {}; // no need for an array arr_jq_TabContents[“Main”] = jq_TabContents_Main; arr_jq_TabContents[“Guide”] = jq_TabContents_Guide; arr_jq_TabContents[“Articles”] = jq_TabContents_Articles; arr_jq_TabContents[“Forum”] = jq_TabContents_Forum; for (var key in arr_jq_TabContents) { console.log(arr_jq_TabContents[key]); } To be safe, it’s a good … Read more

display only 3 foreach result per row

Keep a counter, and output a new table row every 3 images $output=”<table class=”products”>”; $counter = 0; while($info = mysql_fetch_array( $data )) { if( $counter % 3 == 0 ) $output .= ‘<tr>’; $output .= “<td>”; $output .= “<img src=http://localhost/zack/sqlphotostore/images/”.$info[‘photo’] .” width=323px ></img>”; $output .= “<b>Name:</b> “.$info[‘name’]; $output .= “<b>Email:</b> “.$info[’email’]; $output .= “<b>Phone:</b> “.$info[‘phone’].”</td> … Read more

Java Modifying Elements in a foreach

You can’t do that in a foreach loop. for (int i=0; i<copyArray.length;i++) copyArray[i] /= 2; Else you are not assigning it back into the array. Integer objects are immutable by the way so can’t modify them (creating new ones though). Updated from comment: Beware though that there are a few things going on, autoboxing/unboxing for … Read more

stdClass object and foreach loops

It is an array, so you can loop over it easily using foreach: foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message) { echo $message->Reference; } However it is worth noting that PHP’s SoapClient by default appears to return arrays as a PHP array only when there is more than one value in the array – if there is only … Read more