Most voted "foreach" questions
Provides an easy way to iterate over vectors and matrices.
Learn more…324 questions
Sort by count of
-
29
votes6
answers66760
views"foreach" in Javascript
By way of example, in PHP we have the function foreach() (English) which allows us to execute a certain code for each element found in a matrix: <?php $arr = array(1, 2, 3, 4, 5, 6); foreach…
-
27
votes12
answers28737
viewsWhat are the ways to iterate an array in PHP (without foreach)?
Before I am censured by the question, notice beforehand that the purpose of it is simply to level curiosity. I know that the foreach is the most suitable means for this. But I would like to know the…
-
18
votes3
answers713
viewsforeach is a loop or iterator? Or can it be both?
In a video tutorial the instructor stated not to fall for the nonsense of thinking that the foreach is a loop, and it was vehement that he was an iterator. There are cases where we can go through…
-
14
votes3
answers19255
viewsWhat is the difference between while, for, while and foreach?
What is the difference between the while , do while, for and foreach in PHP, because they give the impression that same functionality. All of them can be used to create a loop or has other purposes?…
-
12
votes5
answers3081
viewsHow to decide between using for or foreach?
When should I choose between using the for or foreach? They both do pretty much the same thing, and I always wonder if I’m using the "correct"... My doubt increases when people say to use the for…
-
11
votes3
answers242
viewsWhy do I need to declare a guy inside the "foreach"?
Why in a foreach we always need to declare a variable. Example: for(Pessoa pessoa : pessoas) { //qualquer coisa } In other words, I want to know why it is not possible to do this: Pessoa pessoa;…
-
11
votes2
answers359
views -
10
votes2
answers4980
viewsHow to determine the last element of a list in a foreach?
Given any list, how to know if the foreach is in the last element? There are some ways to do this, the one I see most is to save the last element and check if the current element is the same, but…
-
9
votes2
answers1839
viewsWhich one performs better? For or Foreach+Range?
Of the two forms below, which has a better performance? For: for( $x=1; $x < 31; $x++ ) echo $x . PHP_EOL; Foreach + range: foreach( range(1,30) as $x ) echo $x . PHP_EOL; I know the difference…
-
9
votes2
answers1167
viewsForeach of C# vs Foreach() of EF6
Follows code: ForEach() of EF6: var result = ctx.Table.Where(x => x.User == "João").ToList(); result.ForEach(x => x.Read = true); ctx.SaveChanges(); foreach of the C#: var result =…
-
9
votes2
answers298
viewsHow to check if a value is iterable by foreach in PHP?
In PHP, not just arrays are eternal, but also some specific objects. For example, objects that implement the interface Iterator or IteratorAggregate. Another example is stdClass and ArrayObject,…
-
8
votes2
answers1582
viewsVariable of the foreach loop
When we use a loop loop foreach, the local variable we create to receive the contents of the list in question is passed by value or reference? Ex: foreach (var item in listItems) { //TODO } The…
-
7
votes2
answers218
viewsQuit two PHP Foreachs
I need to iterate several Collections and if I find a condition I should leave more than one iteration loop. Ex.: foreach ($order->getItemCollection() as $item) { foreach…
-
7
votes1
answer230
views -
7
votes2
answers2134
viewsWhich loop is faster for or foreach in C#?
I’ve read articles from some programming languages that loop for is faster than the foreach, and wanted to know if C# has performance differences?
-
7
votes2
answers1573
viewsShow real-time loop result
I’m using a loop to send emails from an array. Every time the loop runs the function sleep(4); is executed. The problem is that php output only happens at the end, ie the lines echo…
-
7
votes2
answers327
views -
6
votes3
answers502
viewsINSERT query does not work inside a loop
So guys, I’m not getting to solve this query problem within a foreach loop not run. At first I thought it was something wrong with Pdo->beginTransaction so I commented on that part and left only…
-
6
votes2
answers286
viewsReturn with all foreach lines
I have the following script: <?php $recursos_data = array( array( "id" => "0", "recurso_nome" => "madeira", "producao" => "%produz%", "estoque" => "200" ), array( "id" => "1",…
-
6
votes2
answers2547
viewsHow to get out of a cycle in C#?
I need to get out of the foreach when the value is equal to 1 foreach (DataGridViewRow dr in dgvValetes) if(dr.Cells["valor"].toString()=="1") //Sair aqui I don’t know how to do...…
-
6
votes2
answers5494
viewsForeach inside of foreach
I need to run a foreach within another foreach in PHP. But it gives me the following error: Parse error: syntax error, Unexpected T_FOREACH referring to the line where the second foreach occurs.…
-
6
votes2
answers1244
views -
6
votes2
answers191
viewsDoes the List<T> Method. Foreach exist?
I’m trying to implement the example of this page, but VS2015 (.NET 4.5) says the method ForEach() there is no class Program { static void Main() { List<String> names = new…
-
6
votes3
answers3023
viewsForeach Array incrementing another array
with a doubt in performing a foreach and incrementing another helper to perform an Insert in the bank, see only: When I send a form via post I have the following array as a result: array (size=2)…
-
5
votes5
answers7269
viewsUse if inside foreach
I’m trying to change the order in which the results appear within a loop using foreach. What I need is for the numbers larger than five to appear first, followed by the rest of the numbers. So I…
-
5
votes1
answer127
views -
5
votes5
answers5699
viewsHow to get the current iteration index of a foreach?
Usually in order to get the current iteration index within a foreach, I do it this way: int i=0; foreach (var elemento in list) { // Qualquer coisa i++; } I believe there are other methods to get…
-
5
votes3
answers894
viewsWhat is the difference between foreach uses in php?
What is the difference between these two means of using the foreach in php? One would be more special than the other, bringing some advantage for example? Method 1: foreach ($arr as $key =>…
-
5
votes1
answer13456
viewsHow to go through attributes of a JSON?
I’m trying to go through attributes of a json, I’m trying this way for (var i = 0, length = r.length; i < length; i++) { for (var i2 = 0,length = r[i].lenght; i2 < length; i2++ ){…
-
5
votes1
answer25706
viewsWarning: Illegal string offset
I’m trying to show in the report the result of a INNER JOIN 3 tables, save the 3 values in a array and make a foreach to go through all the records, but the following error appears: Warning: Illegal…
-
5
votes1
answer803
viewsGetting information from an array within a foreach - PHP
Good afternoon. I’m developing a PHP application using Laravel, and I’m having a hard time. I have a table with the Account name, which has the code of this account, represented by the variable…
-
5
votes2
answers1048
viewsForeach PHP JSON json_decode How to print sub array
Good morning folks! My question is this. I created a foreach to access the data below an API $token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $headers = array('Authorization: Token ' . $token); $ch_subs =…
-
4
votes2
answers339
viewsTrade foreach for for
I need to change the following foreach() by a for(). The problem is that instead of arrays the loop runs through a dictionary. This is the code I want to transform for(): foreach (var joint in…
-
4
votes1
answer123
viewsMethod with foreach only returns false
I have a method that will check whether the debt is equal to zero(divida == "0"). If it is he returns true, else it returns false. Code: for(String divida : dividas){ return divida == "0";…
-
4
votes3
answers408
viewsError listing items from a foreach() array
I have a array with the list of several Brazilian banks and need to take the name of the bank where the code according to what is in the user’s account, only when doing this search for code it…
-
4
votes1
answer299
viewsHelp with PHP repeat structure - PARSER - simple_html_dom.php
I’m doing a parser with simple_html_dom.php where I pull all the links from a given page. I can pull the links and assign to an array, here comes the problem: this page has a maximum display limit…
-
4
votes1
answer228
viewsUsing Foreach in PHP
The builder foreach provides an easy way to iterate on arrays. In several scripts we can observe the following usage: foreach ($array as $value) { $value = $value * 2; } However, in some cases the…
-
4
votes1
answer132
viewsHow to create foreach (PHP/SQL)
I’m trying to send from a form html, a table, with two fields with Array[]. These two fields are Service and Value, I want to add more than one product on form, and he insert into different records…
-
4
votes3
answers3129
viewsHow to list the properties of an object in php using foreach?
I have the following class: class Usuario{ private $nome; private $profissao; function setNome($nome){ $this->nome = $nome; } function getNome(){ return $this->nome; } function…
-
4
votes1
answer838
viewsForeach within Foreach PHP
I have the following code: $row = $twitterusers->selectUserAll(); foreach ($row as $fetch) { $users[] = $fetch; } var_dump($users); It takes all users from the table, but when using the var_dump…
-
3
votes4
answers721
viewsforeach php inside <script>
Could you tell me if the use of this code is wrong: <script type="text/javascript"> <?php foreach ($lista as $key => $value) { $id_indicador = $lista[$key]['id']; ?> var id_indicador…
-
3
votes2
answers1246
viewsScroll through dates by printing a field for each
I’m making a system kind of like a calendar / schedule. In it I have a table semestre who has a field data de incio and one of data fim and I need to go through these dates by printing the name of…
-
3
votes3
answers736
viewsMultiple UPDATE from a dynamic field
I’m trying to catch the ids that come by user-selected checkbox (array), and through these ids, update the table, with the following code: $ocs_imp = $_POST['check_imprime']; $contar =…
-
3
votes1
answer89
viewsMight there be some error in the execution of foreach in this method?
The execution of the code below may occur some error? protected void ImprimeValores(Ilist<int> values) { if (values.Count > 0 && values != null) { foreach(int v in values)…
-
3
votes2
answers607
viewsForeach duplicating the data
I possess 2 foreachs, to search for dates on ViewBags, where I return both in a select. But for each item of the first foreach, it makes a complete loop in the second, thus doubling the values, for…
-
3
votes2
answers1944
viewsRemove comma from the last foreach value in PHP
Good morning, I have a foreach in my system and I need to separate the items pro comma, then the foreach is like this foreach($value as $item){ echo $item . ','} the result obviously comes out:…
-
3
votes1
answer377
viewsForeach behavior with variables by reference
I was doing some tests and I realized that the foreach has a strange behavior. Suppose I have the following array: $array = array('primeiro', 'segundo', 'terceiro'); When I run one foreach using a…
-
3
votes1
answer429
viewsMore than one list in the same loop
I can put more than one list on it loop? for example, you can put more than one variable in a for: for (int a, b; a<10 b<20; a++ b++) { ....... } So I wanted to put more than one list on it…
-
3
votes2
answers95
viewsLabel in loop on Foreach
I’m having difficulty showing information that comes from an object to display on a Label, returning in list form. //Pegando os dados do Rest e armazenando na variável usuários var usuario =…
-
3
votes1
answer392
viewsforeach javascript array
I have the following script: var data = JSON.parse( '<?php echo json_encode($lista_tipo_indicadores) ?>' ); var cores = JSON.parse('<?php echo json_encode($cores) ?>'); // Quantidade de…