How to access first level keys of a 2d array with a foreach loop? [duplicate]
You can access your array keys like so: foreach ($array as $key => $value)
You can access your array keys like so: foreach ($array as $key => $value)
TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly for (const element of theArray) { // …use `element`… } forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) theArray.forEach(element => { // …use `element`… }); a simple … Read more
Updated based on feedback from @BenAston & @trincot Roughly, this is what’s happening in both cases: For loop Set the index variable to its initial value Check whether or not to exit the loop Run the body of your loop Increment the index variable Back to step 2 The only overhead that happens on every … Read more
The problem is that bindParam requires a reference. It binds the variable to the statement, not the value. Since the variable in a foreach loop is unset at the end of each iteration, you can’t use the code in the question. You can do the following, using a reference in the foreach: foreach ($reindex as … Read more
Yes, foreach will call Dispose() on the enumerator if it implements IDisposable.
A unique approach would be to reverse the array and then loop. This will work for non-numerically indexed arrays as well: $items = array( ‘one’ => ‘two’, ‘two’ => ‘two’, ‘three’ => ‘three’ ); $backwards = array_reverse($items); $last_item = NULL; foreach ($backwards as $current_item) { if ($last_item === $current_item) { // they match } $last_item … Read more
foreach($array as $elementKey => $element) { foreach($element as $valueKey => $value) { if($valueKey == ‘id’ && $value == ‘searched_value’){ //delete this particular object from the $array unset($array[$elementKey]); } } }
The reason you’re seeing “string” returned in your first loop is that num refers to the array index, not the value of numarray at that index. Try changing your first loop to alert num instead of typeof num and you’ll see that it spits out 0, 1, and 2, which are the indicies and not … Read more
The strengths and also the weaknesses are pretty well summarized in Stephen Colebourne (Joda-Time, JSR-310, etc) Enhanced for each loop iteration control proposal to extend it in Java 7: FEATURE SUMMARY: Extends the Java 5 for-each loop to allow access to the loop index, whether this is the first or last iteration, and to remove … Read more
This problem is pretty debuggable, an uncommon luxury when you have problems with threads. Your basic tool here is the Debug > Windows > Threads debugger window. Shows you the active threads and gives you a peek at their stack trace. You’ll easily see that, once it gets slow, that you’ll have dozens of threads … Read more