Questions tagged [foreach]

16

votes
7

answer
1k

Views

How to make a list of partial sums using forEach

I have an array of arrays which looks like this: changes = [ [1, 1, 1, -1], [1, -1, -1], [1, 1] ]; I want to get the next value in the array by adding the last value values = [ [1, 2, 3, 2], [1, 0, -1], [1, 2] ]; so far I have tried to use a forEach: changes.forEach(change => { let i = changes.index...
Team Cafe
0

votes
0

answer
18

Views

Nested php foreach in MVC

I have a simple PHP MVC concept and do some nested foreach loops in my controller. In this case, only one of the nested foreach is executed and the other is ignored. Is that the correct way of nested foreach in this case? Controller: $positions = $this->positionModel->getOrderPositionsById($id); for...
Marco
0

votes
2

answer
14

Views

CI PHP: How to hold data in Array while using double Foreach Loop and turn it json encode?

I wanna get data from two table with no relation, i using foreach to fetch all data by selected key and gather data from two table into array. How do i hold data in array while i fetch two table using foreach? im using Codeigniter framework. here is the code: $price = $this->pricing->get_by($hc); //...
Bahtyar
0

votes
3

answer
38

Views

How to access this array inner elements?

EDIT: I'm trying to display category names of my categories table inside select HTML dropdown, but I'm getting Illegal string offset error all the time, no matter if I use $value['category_name'] or $key['category_name']. Categories table just have category_id and category_name columns with two ro...
Limpuls
0

votes
1

answer
13

Views

Loop 2 arrays and compare is there equal values ( Undefined offset)

I have 2 arrays $group_permissions and $result while looping $result array, I compare $group_permissions[$keys]->perm_id from $group_permissions array with $result[$keys]->id from $result array. but record in $group_permissions array are less than records in $result array. so there error is occur...
1

votes
2

answer
55

Views

PHP loop to retrieve each instance of a specific piece of data from within decoded JSON

I'm trying to write a loop that will list all the uid in the JSON data. The uid spans two categories (shirts and pants). Below is what I have so far. Any help is greatly appreciated. Current Code and JSON $url = 'http://foothillertech.com/student/webdesign/2018/2018benrud2/projects/retail/data2.jso...
Mr. B
0

votes
1

answer
37

Views

Instead of forEach how can I use .map(), .reduce(), and .filter() to make my code concise lest brittle?

I am taking an array of objects (let raw), grabbing value by displayName and inserting it into (const defaultCirclePackStructure) by displayName this is for d3 circle packing. Here is Codepen let convertMetricDataToD3 = (arrayMetricData) => { var circlePackData = defaultCirclePackStructure let opera...
Alex Gontcharov
0

votes
0

answer
3

Views

How to fetch Name/Value of Request.Querystring using For Each Loop but in the same order sent?

I was trying to fetch the Name/Value of a querystring using For Each loop, as per @kloarubeek answer shown in → Request.Querystring(variablevalue) possible? as follows: For Each Item in Request.QueryString Response.Write Item & ': ' & Request.QueryString(Item) & '' Next But it returns the items no...
Code Reaper
5

votes
0

answer
72

Views

Usage of for_each in the presence of exceptions? std::exception_list

cppreference documentation https://en.cppreference.com/w/cpp/algorithm/for_each says that: If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the three standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior...
alfC
1

votes
2

answer
28

Views

Why is my Foreach only outputting the first bit of data?

Reasonable new to bots and C# in general. I'm trying to output all the links inner text matching //a[@class='featuredBox but it's only outputting the first bit of data. When I debug, it does, in fact, have loads of values attached to it, so I'm confused as to why this isn't outputting them all? prot...
Gerry Hodson
-1

votes
1

answer
20

Views

Sort on date inside foreach loop with php

I am trying to build a function that displays the reviews from an external json feed in a loop with php. Everything works fine, except the sorting. I would like to sort the items based on date. This is the actual code which i created: ''
Justus
1

votes
1

answer
319

Views

Jquery JSON search object for key and return value

I don't know if this is possible or how to do it but what I want to do loop through some elements with a class, get the id and see if the value of the id is a key name in a JSON object. If it is, I want to get the corresponding value and pass it back to the elements HTML. var options = { 'test1': 's...
AdRock
1

votes
3

answer
4.3k

Views

English to PigLatin Conversion of a Sentence

I am making a program to convert English to PigLatin. However, my solution only seems to work with one word. If I enter in more than ond word, only the last is translated. testing one translation Would simply output: translationway I've looked at some solutions, but most are in the same fashion as m...
DevonRyder
1

votes
2

answer
440

Views

Using foreach in Laravel 5.1

I'm new in Laravel. I'm storing 3 types of user in my users table: admin : user_type_id = 1 agent : user_type_id = 2 farmer : user_type_id = 3 user_type_id column is in the users table. In one of my Blade files, I want to only show the names of the agents. Here is my foreach loop, which is importing...
Noob Coder
1

votes
1

answer
76

Views

Netlogo : inequality doesn't work properly under foreach

It supposes to be a simple code, but I don't know why it doesn't work properly. I want to change a color of non-white turtle back into white if a condition is fulfilled. I put inequality as the condition. for example, if the amount of red turtle > = 5, then [ do something ]. No error message for t...
dya
1

votes
1

answer
480

Views

Infinite loop for each procedurally generated level (C# Unity3D)

I'm trying to procedurally generate a constant number of buildings at random positions in my Unity project. However, I get an infinite loop. This is my code (C#): using UnityEngine; using System.Collections; using System.Collections.Generic; public class GenerateMap : MonoBehaviour { public GameObje...
BlueRine S
1

votes
3

answer
516

Views

compare model in foreach statement

Someone know how to compare a string to a item in a model in a foreach declaration? example: i have this foreach loop @foreach (var item in Model) { //code here } But i want to only display items that equals another string like for example: String example ='Hello' @foreach (var item in Model.where(c...
Arturo Martinez
1

votes
3

answer
860

Views

PowerShell script to go through a directory of csvs and convert them to html

This is a PowerShell question, not a SharePoint question. I'm using a script to grab an inventory of SharePoint features, web parts, etc. It outputs each type of report in the same directory as csv files. So I'll end up with a directory on my computer with the csv files. I'd like to run another Po...
spex5
1

votes
4

answer
118

Views

How to get specific key when i click on values of an array

I have one array (i.e. $a in below) when i click on array href value how to get that specific key of an array. In above e.g. when i click on 'Click Here' value of an array i have three specific keys also. onclick how to get that particular key. when click on http://www.google.com then it return goog...
shopeeon
1

votes
2

answer
41

Views

How to input or insert one name with more products into the database using the power and magic of php?

I am trying to input one name with more products. the scenario here is when I input the Name:_______________ Product:______________ Product:_______________. It will the be inserted into the database and it will display like this: Johnny | Iphone Johnny | Windows Phone Here's my code: Name Product...
user3808774
1

votes
1

answer
150

Views

knockout tbody filter parent / childs

I have the following nested data structure ( viewmodel ): projects ( projectid, project_name ) |--- subprojects ( parent_projectid, subproject_name ) I want to filter the 'subprojects' to 'parent_projectid' which is the id from parent project ( 'projectid' ). The 'tbody' should render the subproject...
Steve Rabbit
1

votes
2

answer
67

Views

how can I parse an XML tag that has an attribute, using perl

Below is a XML file, Pradeep 23 M Coder And the perl code is use XML::Simple; use Data::Dumper; @xml=new XML::Simple; [email protected]>XMLin('data.xml'); print Dumper($data); Now how do you parse if the XML file is John Doe 43 M Analyst Pradeep 23 M HR how can this be done using a foreach loop in perl N...
GRD
1

votes
1

answer
4.4k

Views

Wait for promises inside of a angular.forEach loop

I know this has been asked quite a few times already but after a day of search I still don't get it to work, although it's just like what is shown as a solution everywhere... I have a async request to a database which returns an array of data. For each object in this array I need to start another as...
Yorick Tran
1

votes
1

answer
1.4k

Views

How to get the id of each object from a php foreach loop?

Using a foreach loop in php I'm trying to retrieve data from a database. The idea is to display data, such as a title, from a database using a foreach loop in order to get the data into objects / divs. Here is the example: This is the database (database->posts): I'm using a mysql query to get the da...
NightShift
1

votes
2

answer
41

Views

Merge an array value with another array

I Have 2 arrays: $fields = array('field1'=>'INT', 'field2'=>'STR', 'field3'=>'INT'); $values = array('pour field1', 'pour field2', 'pour field3'); I am looking to merge both of them to get the below result using foreach: foreach($fields as $setK=>$setV){ echo 'k '.$setK.' v '.$setV.''; echo 'Items a...
Oum Alaa
1

votes
2

answer
365

Views

PHP: getting only 1 result from XML collection

I'm trying to read an XML file with PHP but I'm only getting the first result and can't figure out why. XML structure: Some title here Some second title here Pretty basic. This is the code I'm using to getting the results: foreach($XML->data->record as $product) { $title = mysql_real_escape_string($...
MNL
1

votes
1

answer
1.6k

Views

Laravel php foreach Loop get value by key

I think im getting this simple thing confused. I just want to get the value of my key 'weeks' and 'days'. I have tried the following: @foreach($years as $key3 => $year) {{$key3}} @foreach($year as $key2 => $months) {{$key2}} @foreach($months as $key1 => $days) {{$days['weeks']}} {{$days->weeks}} //...
Ricki Moore
1

votes
1

answer
80

Views

Foreach loop through two data sets and use same loop index for both

I have users with activities. Each activity has got some data and also a startDate. Now I need to do something with the data on every single day. Beginning at the startDate of the first activity. So I started doing it in twig but I can also change the logic (array) in PHP. So I thought begin to lo...
caramba
1

votes
1

answer
52

Views

I would like to loop through an array looking for the first value that does not match 2345 string

I am going to rephrase my question so people understand what I am trying to achieve which will make it easier to help me. I have several objects objInputMessage which are found in objInputMessages. Also the object after the first one is what I am considering the first strMessageCaseNumber. The reas...
1

votes
2

answer
625

Views

PHP array empty after pushing in foreach

I am pushing values inside an array in foreach loop and the values seem to be added in the array so the problem is not in the pushing condition. However when I go through that array again, it is empty. Can please anyone explain to me, why this code foreach ($allItems as $pair) { for ($i = 0; $i < co...
dannymo
1

votes
1

answer
56

Views

Multiple preg_match_all result in foreach

I would like to replace each decimal number for later using in Javascript. so This is my code: if (preg_match_all('#[0-9]+(\.[0-9]{1,8})#', $operationvalue_new2, $result)) { foreach ($result[0] as $number_element) {...
Olaf K.
1

votes
4

answer
339

Views

PHP foreach loop accessing different parts of multidimensional array

I am still trying to understand how the php foreach loop works with accessing certain parts of it. I have an array setup like this: $product = Array ( [SX- Light weight] => Array ( [bronze] => 4 [silver] => 5 [gold] => 6 ) [SS- Light weight] => Array ( [bronze] => 4 [silver] => 5 [gold] => 6 ) ) I h...
Packy
1

votes
1

answer
539

Views

Foreach code works for %do% but not for %dopar%

This works normally on my computer: registerDoSNOW(makeCluster(2, type = 'SOCK')) foreach(i = 1:M,.combine = 'c') %dopar% { sum(rnorm(M)) } So I can say that I can run parallelized code on this computer, right? Ok. I have a piece of code that I wish to run on parallel with foreach. It runs perfectly...
Catarina Zidde
1

votes
1

answer
925

Views

Iterating through multiple lists using single for-each loop in java

I have multiple lists created like below static List dbName = new ArrayList(); static List dbServerName = new ArrayList(); static List lDbName = new ArrayList(); static List lServerName = new ArrayList(); static List serverName = new ArrayList(); static List dbServerNameSecondary = new ArrayList();...
wandermonk
1

votes
1

answer
537

Views

how to combine doMPI and doMC on a cluster?

I was used to do parallel computation with doMC and foreach and I have now access to a cluster. My problem is similar to this one Going from multi-core to multi-node in R but there is no response on this post. Basically I can request a number of tasks -n and a number of cores per task -c to my batch...
clemlaflemme
1

votes
2

answer
119

Views

Php Array multidimensional associative compare value

Hi I have such a structure array. I would like compare in loop such a condition. if(dataInto[0]>=MyDataTypeFromInput && dataOut[0] [ 0 => '2016-07-14 14:50' 1 => '2016-03-24 14:00' 2 => '2016-03-03 06:30' ] 'room' => [ 0 => 13 1 => 14 2 => 14 ] 'dataOut' => [ 0 => '2016-07-14 18:10' 1 => '2016-03-...
Demolog
1

votes
2

answer
32

Views

Transform numeric array in associative in order to access it from html template

Alright, my data gets stored in a file using the same pattern: id|title|content I created a function which explodes the entries by the delimiter '|' into an array. function get_entries(){ $retAr = array(); $data = file_get_contents('db/entries.log'); $lines = explode('\n', $data); //create array sep...
denii
1

votes
2

answer
704

Views

Comparing PHP variable to values in text file

I'm trying to validate that a php variable value exists in a text file then echo that value. Using the code below, my if statement is only true if the value in the variable is equal to the last value in the text file. If the value in the variable is equal to the first, second, third, etc values the...
brandozz
1

votes
2

answer
371

Views

Looping does not work in JSON serialization using Web Api Controller ASP.NET MVC

I am trying to Deserilize json data from wikipedia api. I want to get only the Image information and then serilize again for my own formatted JSON Data. But the problem in my code is that it shows only the last data from the list, Not the entire data.The foreach loop is not working. My Code to Deser...
harry.luson
1

votes
1

answer
1.3k

Views

How to pass variables into Write-Host with pipeline

I have the following bit of script: try { Get-ADUser -Properties Department -Filter $Filter | Where-Object {$_.Department -eq $old} | Set-ADUser -Department $new } catch { Write-Host 'Error occured' -BackgroundColor Red -ForegroundColor Black } Which changes a given set of user's department to the...
Bassie

View additional questions