Search Results

Search found 4868 results on 195 pages for 'foreach'.

Page 2/195 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • c# string.replace in foreach loop

    - by paradox
    Somehow I can't seem to get string replacement within a foreach loop in C# to work. My code is as follows : foreach (string s in names) { s.Replace("pdf", "txt"); } Am still quite new to LINQ so pardon me if this sounds amateurish ;)

    Read the article

  • How to loop 3 dimension array using foreach PHP

    - by vzhen
    Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv foreach ($stories as $key => $story){ //echo "<br />"; foreach($story as $subkey => $subvalue){ echo $subvalue."<br />"; foreach($subvalue as $key => $subsubvalue){ echo $subsubvalue."<br />"; } } } Array ( [270] => Array ( [uid] => 36 [user_email] => [email protected] [sid] => 270 [story_name] => Story C [photo_url] => Array ( [0] => story_photos/2012/0322/361332381418153311.jpg [1] => story_photos/2012/0322/361332393792911587.jpg ) [photo_added_date] => Array ( [0] => 1332381418 [1] => 1332393792 ) ) [269] => Array ( [uid] => 36 [user_email] => [email protected] [sid] => 269 [story_name] => Story B [photo_url] => Array ( [0] => story_photos/2012/0322/361332381406580761.jpg ) [photo_added_date] => Array ( [0] => 1332381406 ) ) [268] => Array ( [uid] => 36 [user_email] => [email protected] [sid] => 268 [story_name] => Story A [photo_url] => Array ( [0] => story_photos/2012/0322/361332381393552719.jpg ) [photo_added_date] => Array ( [0] => 1332381393 ) ) )

    Read the article

  • Perl, foreach order

    - by Mike
    Does Perl's foreach loop operator require that the list items be presented in order? For example my @a=(1,2,3); foreach my $item (@a) { print $item; } will always print 1 2 3? I suspect so, but I can't find it documented.

    Read the article

  • need help passing multiple variables from foreach loop to test in switch case statement

    - by Brad
    $list_of_groups = array("FACULTY","STAFF"); foreach ($list_of_groups as $i => $group) { $user_in_group = $adldap->user_ingroup($username,$group); print "<h2>Group: ".$group." user in group? ".$user_in_group."</h2>"; // if 1, means yes } Need to print run the appropriate function based on what returns true. There are user's that are members of both FACULTY and STAFF groups, so I want to check for those users and display the appropriate content for them. So if the user is both faculty and staff, then display this, if they are only of staff, display that, same for faculty, might not make sense, but I will write out some code "in theory" that will help you understand what I am trying to do switch(Get group membership of user) { case "FACULTY": print "Faculty group member"; break; case "STAFF": print "Staff group member"; break; case "FACULTY and STAFF": print "Member of both faculty and staff"; break; } I am unsure on how it will check if they are members of both groups and run that thru the case statement to display the appropriate message. The foreach look currently runs thru every group the user belongs to, prints out the ones from the $list_of_groups and the number 1 to the right of it, signifying they belong to it. The problem I have is trying to use that information to run thru the case statement, I am unsure of how to go about that. This is what it prints out for the user currently passed thru the foreach loop: Group: FACULTY user in group? 1 Group: STAFF user in group? 1 Any help is appreciated.

    Read the article

  • Invalid argument supplied for foreach() using adldap

    - by Brad
    I am using adldap http://adldap.sourceforge.net/ And I am passing the session from page to page, and checking to make sure the username within the session is a member of a certain member group, for this example, it is the STAFF group. <?php ini_set('display_errors',1); error_reporting(E_ALL); require_once('/web/ee_web/include/adLDAP.php'); $adldap = new adLDAP(); session_start(); $group = "STAFF"; //$authUser = $adldap->authenticate($username, $password); $result=$adldap->user_groups($_SESSION['user_session']); foreach($result as $key=>$value) { switch($value) { case $group: print '<h3>'.$group.'</h3>'; break; default: print '<h3>Did not find specific value: '.$value.'</h3>'; } if($value == $group) { print 'for loop broke'; break; } } ?> It gives me the error: Warning: Invalid argument supplied for foreach() on line 15, which is this line of code: foreach($result as $key=$value) { When I uncomment the code $authUser = $adldap-authenticate($username, $password); and enter in the appropriate username and password, it works fine, but I shouldn't have to, since the session is valid, I just want to see if the username stored within the valid_session is apart of the STAFF group. Why would it be giving me that problem?

    Read the article

  • Foreach loop returning null values in PHP?

    - by Jascha
    Hello, I have a pretty simple problem. Basically I have an array called $list that is a list of titles. If I do a print_r($list) I get these results: Array ( [0] => Another New Title [1] => Awesome Movies and stuff [2] => Jascha's Title ) Now, I'm running a foreach loop to retrieve their values and format them in an <ul> like so... function get_film_list(){ global $categories; $list = $categories->get_film_list(); if(count($list)==0){ echo 'No films are in this category'; }else{ echo '<ul>'; foreach($list as $title){ echo '<li>' . $title . '<li>'; } echo '</ul>'; } } The problem I'm having is my loop is returning two values per value (is it the key value?) The result of the preceding function looks like this: Another New Title   Awesome Movies and stuff   Jascha's Title   I even tried: foreach($list as $key => $title){ echo '<li>' . $title . '<li>'; } With the same results: Another New Title   Awesome Movies and stuff   Jascha's Title   What am I missing here? Thanks in advance.

    Read the article

  • Foreach over a collection of IEnumerables

    - by sdr
    I have 3 IEnumerables that I want to iterate over. I want to do something like this: IEnumerable<Car> redCars = GetRedCars(); IEnumerable<Car> greenCars = GetGreenCars(); IEnumerable<Car> blueCars = GetBlueCars(); foreach(Car c in (redCars + greenCars + blueCars)) { c.DoSomething(); } ... The best way I can think of is: ... List<Car> allCars = new List(); allCars.AddRange(redCars); allCars.AddRange(greenCars); allCars.AddRange(blueCars); foreach(car in allCars) { ... } ... Is there a more concise way to do this? Seems like combinding IEnumberables should be trivial.

    Read the article

  • How to build boost foreach cycle

    - by Ockonal
    Hi guys! I have some abstract class called IClass (has pure virtual function). There are some classes which inherit IClass: CFirst, CSecond. I want to add objects of classes which inherit into boost::ptr_vector: class IClass { virtual void someFunc() = 0; }; class CFirst : public IClass { }; class CSecond : public IClass { }; boost::ptr_vector<IClass> objectsList; objectsList.push_back(new CFirst()); objectsList.push_back(new CSecond()); And now my goal is to call function (which is declarated in IClass) from all objects in objectsList. I'd prefer to use BOOST_FOREACH: foreach(IClass tempObj, objectsList) { tempObj.someFunc(); } The problem is that IClass - abstract class, so I can't make instance of it for the foreach-cycle. What should I do?

    Read the article

  • Change write-host output color based on foreach if elseif outcome in Powershell

    - by Emo
    I'm trying to change the color of write-host output based on the lastrunoutcome property of SQL Server jobs in Powershell....as in...if a job was successfull, the output of lastrunoutcome is "Success" in green....if failed, then "Failed" in red. I have the script working to get the desired job status...I just don't know how to change the colors. Here's what I have so far: # Check for failed SQL jobs on multiple servers [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null foreach ($svr in get-content "C:\serverlist2.txt") { $a = get-date $BegDate = (Get-Date $a.AddDays(-1) -f d) + " 12:00:00 AM" $BegDateTrans = [system.datetime]$BegDate write-host $svr $srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "$svr" $srv.jobserver.jobs | where-object {$_.lastrundate -ge $BegDateTrans -and $_.Name -notlike "????????-????-????-????-????????????"} | format-table name,lastrunoutcome,lastrundate -autosize foreach ($_.lastrunoutcome in $srv.jobserver.jobs) { if ($_.lastrunoutcome = 0) { -forgroundcolor red } else {} } } This seems to be the closest I've gotten...but it's giving me an error of ""LastRunOutcome" is a ReadOnly property." Any help would be greatly appreciated! Thanks! Emo

    Read the article

  • Append a parent element to arrays in PHP for a foreach loop

    - by Erik Berger
    print_r of $data = json_decode($src, true); returns data that looks like this: Array ( [0] => Array ( [var1] => blah [var2] => foo ) [1] => Array ( [var1] => lorem [var2] => ipsum ) // goes down to [1936] ) I want to build an html table that shows var1 and var2 next to each other in the same row. I know do to this I need a foreach statement like foreach($data['items'] as $item) but the problem I think I am having is that my many arrays aren't the child of one thing like 'items', right? I looked into array_push but couldn't figure it out. Can someone help my orphaned parentless arrays?

    Read the article

  • php foreach as variable

    - by user167850
    I'd like to use foreach to loop though an array list and add an element to each array. $tom = array('aa','bb','cc'); $sally = array('xx','yy','zz'); $myArrays = array('tom','sally'); foreach($myArrays as $arrayName) { ${$arrayName}[] = 'newElement'; } Is the use of ${$arrayName}[] the best way to do this? Is there another option rather than using curly braces? It currently works but I'm just wondering if there is a better alternative. Thanks

    Read the article

  • Nested Array with one foreach Loop?

    - by streetparade
    I need to have access to a array which looks like this. Array ( [0] => Array ( [54] => Array ( [test] => 54 [tester] => result ) ) ) foreach($array as $key=>$value) { echo $key;// prints 0 echo $value;// prints Array /* now i can iterate through $value but i dont want it solve that way example: foreach($value as $k=>$v) { echo $k;//prints test echo $v; //prints 54 } */ } How can iterate just once ? to get the values of test and tester? I hope i could explain my problem clear

    Read the article

  • PHP Casting as Object type in foreach Loop

    - by Coulton
    Within the following code, $quiz_object->personalities contains an array of Personality() objects. // Loop through each personality that exists for the quiz foreach($quiz_object->personalities AS $existing_personality) { // Show all of the existing personalities echo $existing_personality->GetQuizMakerPersonalityHTML(); } How do I "cast" (I think that's the right word) my variable $existing_personality within the foreach loop as the object type? I wish to do this so that when I type $existing_personality->, I get the list of public functions available for that object type. At the moment, Zend doesn't know that it refers to a Personality object within the loop.

    Read the article

  • PHP Variable Passing in Foreach on same page

    - by tooly228
    I've been struggling this for a while and I simply can't figure this out. Here is my code: <?php foreach($list as $id =>$name) { echo("<td style=\"vertical-align:middle;\"> <a href=\"item=$id#confirm\" role=\"button\" data-toggle=\"modal\"> Buy</a></td></tr>"); }?> <html> <div class="modal small hide fade" id="confirm" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true"> <a href="redeem.php?item=<?php echo $id; ?>"><button class="btn btn-danger"> Buy</button></a></div> The main issue here is that the $id from the foreeach is not the same as the $id in the div class link. Instead the link is the end value of the foreach list.

    Read the article

  • Using xsl:variable in a xsl:foreach select statment

    - by Nefariousity
    I'm trying to iterate through an xml document using xsl:foreach but I need the select=" " to be dynamic so I'm using a variable as the source. Here's what I've tried: ... <xsl:template name="SetDataPath"> <xsl:param name="Type" /> <xsl:variable name="Path_1">/Rating/Path1/*</xsl:variable> <xsl:variable name="Path_2">/Rating/Path2/*</xsl:variable> <xsl:if test="$Type='1'"> <xsl:value-of select="$Path_1"/> </xsl:if> <xsl:if test="$Type='2'"> <xsl:value-of select="$Path_2"/> </xsl:if> <xsl:template> ... <!-- Set Data Path according to Type --> <xsl:variable name="DataPath"> <xsl:call-template name="SetDataPath"> <xsl:with-param name="Type" select="/Rating/Type" /> </xsl:call-template> </xsl:variable> ... <xsl:for-each select="$DataPath"> ... The foreach threw an error stating: "XslTransformException - To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function." When I use the msxsl:node-set() function though, my results are blank. I'm aware that I'm setting $DataPath to a string, but shouldn't the node-set() function be creating a node set from it? Am I missing something? When I don't use a variable: <xsl:for-each select="/Rating/Path1/*"> I get the proper results. Here's the XML data file I'm using: <Rating> <Type>1</Type> <Path1> <sarah> <dob>1-3-86</dob> <user>Sarah</user> </sarah> <joe> <dob>11-12-85</dob> <user>Joe</user> </joe> </Path1> <Path2> <jeff> <dob>11-3-84</dob> <user>Jeff</user> </jeff> <shawn> <dob>3-5-81</dob> <user>Shawn</user> </shawn> </Path2> </Rating> My question is simple, how do you run a foreach on 2 different paths?

    Read the article

  • nested foreach in PHP problem

    - by Neuquino
    I'm having problems to iterate twice on the same array: <? $indice=0 ?> <?php foreach ($comisiones as $comision1):?> <tr> <td><?php echo ++$indice ?></td> <td><?php echo tag('select',array('name'=>'comision_'.$indice),true)?> <?php foreach ($comisiones as $comision2):?> <option value="<?php echo $comision2->getId()?>"> <?php echo $comision2->getNombre()." - ".$comision2->getDescripcion()?> </option> <?php endforeach?> </select> </td> </tr> <?php endforeach?> The above code prints: And I'm expecting to see something like this (labels of the combos in the images are not the same, but I think the idea is clear): Thanks in advance

    Read the article

  • Creating foreach loops using Code Igniter controller and view

    - by Tim
    Hello, This is a situation I have found myself in a few times and I just want clear it up once and for all. Best just to show you what I need to do in some example code. My Controller function my_controller() { $id = $this->uri->segment(3); $this->db->from('cue_sheets'); $this->db->where('id', $id); $data['get_cue_sheets'] = $this->db->get(); $this->db->from('clips'); $this->db->where('sheet_id', ' CUE SHEET ID GOES IN HERE ??? '); $data['get_clips'] = $this->db->get(); $this->load->view('show_sheets_and_clips', $data); } My View <?php if($get_cue_sheets->result_array()) { ?> <?php foreach($get_cue_sheets->result_array() as $sheetRow): ?> <h1><?php echo $sheetRow['sheet_name']; ?></h1> <br/> <?php if($get_clips->result_array()) { ?> <ul> <?php foreach($get_clips->result_array() as $clipRow): ?> <li><?php echo $clipRow['clip_name']; ?></li> <?php endforeach; ?> </ul> <?php } else { echo 'No Clips Found'; } ?> <?php endforeach; ?> <?php } ?> The problem I am having is the concept of passing data back to the controller from the view as I am sending the Database Queries off to the view as an array, when I really need to get some more information as to which sheet ID I am looking for to show the relevant clips. I hope this makes sense to someone out there. Thanks, Tim

    Read the article

  • CodeIgniter: problem using foreach in view

    - by krike
    I have a model and controller who gets some data from my database and returns the following array Array ( [2010] => Array ( [year] => 2010 [months] => Array ( [0] => stdClass Object ( [sales] => 2 [month] => Apr ) [1] => stdClass Object ( [sales] => 1 [month] => Nov ) ) ) [2011] => Array ( [year] => 2011 [months] => Array ( [0] => stdClass Object ( [sales] => 1 [month] => Nov ) ) ) ) It shows exactly what it should show but the key's have different names so I have no idea on how to loop through the years using foreach in my view. Arrays is something I'm not that good at yet :( this is the controller if you need to know: function analytics() { $this->load->model('admin_model'); $analytics = $this->admin_model->Analytics(); foreach ($analytics as $a): $data[$a->year]['year'] = $a->year; $data[$a->year]['months'] = $this->admin_model->AnalyticsMonth($a->year); endforeach; echo"<pre style='text-align:left;'>"; print_r($data); echo"</pre>"; $data['main_content'] = 'analytics'; $this->load->view('template_admin', $data); }//end of function categories()

    Read the article

  • PHP sql with foreach loop variable problem

    - by anthony
    This is really getting frustrating. I have a text file that I'm reading for a list of part numbers that goes into an array. I'm using the following foreach function to search a database for matching numbers. $file = file('parts_array.txt'); foreach ($file as $newPart) { $sql = "SELECT products_sku FROM products WHERE products_sku='" . $newPart . "'"; $rs = mysql_query($sql); $num_rows = mysql_num_rows($rs); echo $num_rows; echo "<br />"; } The problem is I'm getting 0 rows returned from mysql_num_rows. I can type the sql statement without the variable and it works perfectly. I can even echo out the sql statement from this script, copy and paste the statement from the browser and it works. But, for some reason I'm not getting any records when I'm using the variable. I've used variables in sql statements tons of times, but this really has me stumped.

    Read the article

  • Foreach loop and tasks.

    - by Scott Chamberlain
    I know from the codeing guidlines that I have read you should not do for (int i = 0; i < 5; i++) { Task.Factory.StartNew(() => Console.WriteLine(i)); } Console.ReadLine(); as it will write 5 5's, I understand that and I think i understand why it is happening. I know the solution is just to do for (int i = 0; i < 5; i++) { int localI = i; Task.Factory.StartNew(() => Console.WriteLine(localI)); } Console.ReadLine(); However is something like this ok to do? foreach (MyClass myClass in myClassList) { Task.Factory.StartNew(() => myClass.DoAction()); } Console.ReadLine(); Or do I need to do the same thing I did in the for loop. foreach (MyClass myClass in myClassList) { MyClass localMyClass = myClass; Task.Factory.StartNew(() => localMyClass.DoAction()); } Console.ReadLine();

    Read the article

  • Generating new tasks in a foreach loop

    - by Scott Chamberlain
    I know from the codeing guidlines that I have read you should not do for (int i = 0; i < 5; i++) { Task.Factory.StartNew(() => Console.WriteLine(i)); } Console.ReadLine(); as it will write 5 5's, I understand that and I think i understand why it is happening. I know the solution is just to do for (int i = 0; i < 5; i++) { int localI = i; Task.Factory.StartNew(() => Console.WriteLine(localI)); } Console.ReadLine(); However is something like this ok to do? Task currentTask = myFirstTask; currentTask.Start(); foreach (Task task in _TaskList) { currentTask.ContinueWith((antecendent) => { if(antecendent.IsCompleated) { task.Start(); } else //do error handling; }); currentTask = task; } } or do i need to do this? Task currentTask = myFirstTask; foreach (Task task in _TaskList) { Task localTask = task; currentTask.ContinueWith((antecendent) => { if(antecendent.IsCompleated) { localTask.Start(); } else //do error handling; }); currentTask = task; }

    Read the article

  • php in_array() inside a foreach

    - by 432skronker
    I am having issues with using in_array() inside a foreach loop. Not sure if this is even possible or if I am doing something ridiculous where there are better ways. What I want to do is go through all the items and if their item id matches one thats in the array, return true and add the price of the item to a runninng total. $price = 0; $result = false; $array = array(1533, 2343, 2333); foreach($order['items'] as $item){ if(in_array($item['Item'], $array)){ $result = true; $price += $item['Price']; } } **UPDATED** Here is the order array [items] => Array ( [0] => Array ( [Item] => 139957 [OrderID] => 16025 [SizeID] => 24 [Price] => 46.00 ) [1] => Array ( [Item] => 2343 [OrderID] => 16025 [SizeID] => 12 [Price] => 32.00 ) ) [data] => Array ( )

    Read the article

  • ForEach loop: Output something different on every second result

    - by Wade D Ouellet
    I have two for each loops and I am trying to output something different for each second result: foreach ($wppost as $wp) { $wp_title = $wp->post_title; $wp_date = strtotime($wp->post_date); $wp_slug = $wp->post_name; $wp_id = $wp->ID; // Start Permalink Template $wp_showurl = $wp_url; $wp_showurl = str_replace("%year%", date('Y', $wp_date), $wp_showurl); $wp_showurl = str_replace("%monthnum%", date('m', $wp_date), $wp_showurl); $wp_showurl = str_replace("%day%", date('d', $wp_date), $wp_showurl); $wp_showurl = str_replace("%hour%", date('H', $wp_date), $wp_showurl); $wp_showurl = str_replace("%minute%", date('i', $wp_date), $wp_showurl); $wp_showurl = str_replace("%second%", date('s', $wp_date), $wp_showurl); $wp_showurl = str_replace("%postname%", $wp_slug, $wp_showurl); $wp_showurl = str_replace("%post_id%", $wp_id, $wp_showurl); // Stop Permalink Template $wp_posturl = $blog_address . $wp_showurl; echo '<li><a href="'.$wp_posturl.'" title="'.$wp_title.'">'.$wp_title.'</a></li>'; } For this one I want it to echo li class="even" instead of just li for each second result. I think this will have to be changed to a for loop to accomplished this but I am not sure how that is done without breaking it. Exact same for this one: if(is_array($commenters)) { foreach ($commenters as $k) { ?><li><?php if($ns_options["make_links"] == 1) { $url = ns_get_user_url($k->poster_id); if(trim($url) != '') { echo "<a href='" . $url . "'>"; } } if($ns_options["make_links"] == 2) { $url = ns_get_user_profile($k->poster_id); echo "<a href='" . $url . "'>"; } $name = $bbdb->get_var(" SELECT user_login FROM $bbdb->users WHERE ID = $k->poster_id"); echo ns_substr_ellipse($name, $ns_options["name_limit"]); if(trim($url) != '' && $ns_options["make_links"] == 1) { echo "</a>"; } if($ns_options["make_links"] == 2) { echo "</a>"; } if ($ns_options["show_posts"] == 1) { echo " (" . $k->num_posts . ")\n"; } echo $ns_options["end_html"] . "\n"; unset($url); } } else { ?></li><?php } Thanks, Wade

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >