How do I jump out of a foreach loop in C#?

foreach (string s in sList) { if (s.equals(“ok”)) return true; } return false; Alternatively, if you need to do some other things after you’ve found the item: bool found = false; foreach (string s in sList) { if (s.equals(“ok”)) { found = true; break; // get out of the loop } } // do stuff … Read more

Performance of foreach, array_map with lambda and array_map with static function

Its interesting to run this benchmark with xdebug disabled, as xdebug adds quite a lot of overhead, esp to function calls. This is FGM’s script run using 5.6 With xdebug ForEach : 0.79232501983643 MapClosure: 4.1082420349121 MapNamed : 1.7884571552277 Without xdebug ForEach : 0.69830799102783 MapClosure: 0.78584599494934 MapNamed : 0.85125398635864 Here there is only a very small … Read more

jquery loop on Json data using $.each

var data = [ {“Id”: 10004, “PageName”: “club”}, {“Id”: 10040, “PageName”: “qaz”}, {“Id”: 10059, “PageName”: “jjjjjjj”} ]; $.each(data, function(i, item) { alert(data[i].PageName); }); $.each(data, function(i, item) { alert(item.PageName); }); these two options work well, unless you have something like: var data.result = [ {“Id”: 10004, “PageName”: “club”}, {“Id”: 10040, “PageName”: “qaz”}, {“Id”: 10059, “PageName”: “jjjjjjj”} … Read more

Possible to iterate backwards through a foreach?

If you are on .NET 3.5 you can do this: IEnumerable<int> enumerableThing = …; foreach (var x in enumerableThing.Reverse()) It isn’t very efficient as it has to basically go through the enumerator forwards putting everything on a stack then pops everything back out in reverse order. If you have a directly-indexable collection (e.g. IList) you … Read more

foreach with index [duplicate]

I keep this extension method around for this: public static void Each<T>(this IEnumerable<T> ie, Action<T, int> action) { var i = 0; foreach (var e in ie) action(e, i++); } And use it like so: var strings = new List<string>(); strings.Each((str, n) => { // hooray }); Or to allow for break-like behaviour: public static … Read more

PHP foreach change original array values [duplicate]

In PHP, passing by reference (&) is … controversial. I recommend not using it unless you know why you need it and test the results. I would recommend doing the following: foreach ($fields as $key => $field) { if ($field[‘required’] && strlen($_POST[$field[‘name’]]) <= 0) { $fields[$key][‘value’] = “Some error”; } } So basically use $field … Read more

Why am I not getting a java.util.ConcurrentModificationException in this example?

Here’s why: As it is says in the Javadoc: The iterators returned by this class’s iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException. This check … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)