foreach

php foreach counting new lines (\n)

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-19 07:41:34
问题 If I have a piece of code that works like this: $i = 0; $names = explode(",", $userInput); foreach($names as $name) { $i++; } It works perfectly, provided the user has placed a comma after each name entered into the html textarea this comes from. But I want to make it more user friendly and change it so that each name can be entered on a new line and it'll count how many lines the user has entered to determine the number of names entered into the field. So I tried: $i = 0; $names = explode("

Replace for-each loop with lambda expression

纵然是瞬间 提交于 2021-02-19 06:25:09
问题 I'm just refactoring some of my old projects to use features of Java 8. int counter = 1; for (Checker checker : checkers) { if (counter < checkers.size()) { checker.setNextChecker(checkers.get(counter++)); } } Here's kinda Chain of Resp pattern. And I need to set next checker for every checker in the list, excluding the last one. Still can't find the way to use Stream API here :) 回答1: Using IntStream.range : IntStream.range(1, checkers.size()) .forEach(i -> checkers.get(i-1).setNextChecker

How to make a foreach of an object in javascript?

心已入冬 提交于 2021-02-18 19:01:06
问题 I am currently building a mashup in Qlik Sense in JavaScript and jQuery. I have made some data selection and I have put them in a variable object : var CurrentSelec = app1.selectionState().selections; console.log(CurrentSelec);` console.log(typeof CurrentSelec); This is what I get on my browser console : I'm trying to show the qSelected value in a Foreach : I have tried with Javascript : for(var index in CurrentSelec) { console.log(index.qSelected); } I have tried with jQuery: $.each

Foreach returns only one row

谁都会走 提交于 2021-02-17 07:20:13
问题 I have foreach loop, but it only returns a single option in select menu. Vardump shows multiple results. What am I missing? $vars = explode(';', $this->settings['address']); foreach ($vars as $row) { return array( 'title' => $this->name, 'options' => array( array( 'id' => 'option_1', 'icon' => $this->settings['icon'], 'name' => 'Option 1', 'description' =>'', 'fields' => '<select name="address" class="form-control">' . PHP_EOL . '<option value="'.$row.'" >'.$row.'</option>' . PHP_EOL . '<

foreach loop returns only one item from the array

眉间皱痕 提交于 2021-02-17 07:12:25
问题 I have an array that I loop through with for each loop it returns only the first iteration but if I change it to echo it prints all of them to the screen, new to PHP not sure why is it acting this way tried looking for an answer but did not find one. the code below: function getData($values){ foreach ($values as $key => $value){ return "<p>". $key . " " . $value ."</p></br>"; } } $SubmitedResult->SerialisedForm = getData($data); 回答1: return always exits the function and returns its argument.

c:forEach not working in tomcat 7

时光怂恿深爱的人放手 提交于 2021-02-17 05:55:08
问题 I created simple page using c:forEach tag. Its working in tomcat 6. But not working tomcat 7 . Developed simple web application using JSF 2.0. I run my code in tomcat 6. its working. I deployed in tomcat 7. Its not working. c:forEach tag result not appearing. welcomeJSF.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core"

How to append data to file using file_put_contents()?

自闭症网瘾萝莉.ら 提交于 2021-02-17 02:47:13
问题 I have an android app that sends multiple data from okhttp3 but i can't find a way to log all the data sent in php..My current log only houses the last record(Show below). My best guess is that the php file data is being overwritten until the last record..How can I log all the data sent? And yes all the data is being sent out from the android app... index.php if (isset($_POST)) { file_put_contents("post.log",print_r($_POST,true)); } Sample post.log Array ( [date] => 02 Aug, 12:22 [company] =>

pass a variable to foreach function

雨燕双飞 提交于 2021-02-16 15:53:10
问题 Hi I want pass antwoord to opleidingArray.forEach(haalScoresOp, antwoord); So I can use it in the HaalScoresOp function. I cannot get this to work. I also tried binding but this does not function. I am getting antwoord is not defined as an error. var antwoordenPerVraag = [2,1,3]; console.log(VragenEnScores.vragen[0].opleidingen[0]); antwoordenPerVraag.forEach(berekenEindresultaten); function berekenEindresultaten(item, index) { var opleidingArray = VragenEnScores.vragen[index].opleidingen;

Scala - difference between for each loops

本秂侑毒 提交于 2021-02-16 06:12:46
问题 Is there any difference between the two following statements. They achieve the same end, correct? Do they compile to the same Java code? Is there any performance difference between them or is it just a matter of preference/readability? for (thing <- things) { doSome(thing) } things.foreach( thing => doSome(thing) ) 回答1: for comprehensions are defined as simple syntactic translations. That's extremely important, because that allows any object to work with for comprehensions, it just has to

How can I speed up this For Each loop in VBA?

十年热恋 提交于 2021-02-15 06:46:37
问题 I have an Worksheet_Change macro that hides/unhides rows depending on the choice a user makes in a cell with a data validation list. The code takes a minute to run. It's looping over c.2000 rows. I'd like it to take closer to a few seconds so it becomes a useful user tool. Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) 'Exit the routine early if there is an error On Error GoTo EExit 'Manage Events Application.ScreenUpdating = False Application.DisplayAlerts = False