logo
down
shadow

multiple foreach loops inside while loop


multiple foreach loops inside while loop

By : user2956515
Date : November 22 2020, 10:56 AM
Any of those help is it possible to include multiple "foreach" statements inside any of the looping constructs like while or for ... i want to open the .wav files from two different directories simultaneously so that i can compare files from both. here is what i am trying to so but it is certainly wrong.. any help in this regard is appreciated. , Something like this since you only need to validate same file names:
code :
IEnumerable<string> fileEntries1 = Directory.GetFiles(folder1, "*.wav").Select(x => Path.GetFileName(x));
IEnumerable<string> fileEntries2 = Directory.GetFiles(folder2, "*.wav").Select(x => Path.GetFileName(x));

IEnumerable<string> filesToIterate = (fileEntries1.Count() > fileEntries2.Count()) ? fileEntries1 : fileEntries2;
IEnumerable<string> filesToValidate = (fileEntries1.Count() < fileEntries2.Count()) ? fileEntries1 : fileEntries2;

// Iterate the bigger collection
foreach (string fileName in filesToIterate)
{
    // Find the files in smaller collection
    if (filesToValidate.Contains(fileName))
    {
        // Get actual file and compare
    }
    else
    {
        // File does not exist in another list. Handle appropriately
    }
}
List<string> fileEntries1 = new List<string>(Directory.GetFiles(folder1, "*.wav"));

List<string> fileEntries2 = new List<string>(Directory.GetFiles(folder2, "*.wav"));

List<string> filesToIterate = (fileEntries1.Count > fileEntries2.Count) ? fileEntries1 : fileEntries2;
filesToValidate = (fileEntries1.Count < fileEntries2.Count) ? fileEntries1 : fileEntries2;

string iteratorFileName;
string validatorFilePath;

// Iterate the bigger collection
foreach (string fileName in filesToIterate)
{
    iteratorFileName = Path.GetFileName(fileName);

    // Find the files in smaller collection
    if ((validatorFilePath = FindFile(iteratorFileName)) != null)
    {
        // Compare fileName and validatorFilePath files here
    }
    else
    {
        // File does not exist in another list. Handle appropriately
    }
}
static List<string> filesToValidate;

private static string FindFile(string fileToFind)
{
    string returnValue = null;

    foreach (string filePath in filesToValidate)
    {
        if (string.Compare(Path.GetFileName(filePath), fileToFind, true) == 0)
        {
            // Found the file
            returnValue = filePath;
            break;
        }
    }

    if (returnValue != null)
    {
        // File was found in smaller list. Remove this file from the list since we do not need to look for it again
        filesToValidate.Remove(returnValue);    
    }

    return returnValue;    
}


Share : facebook icon twitter icon
Foreach loops inside foreach loops giving me problems in codeigniter

Foreach loops inside foreach loops giving me problems in codeigniter


By : user2249163
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have a foreach and then a foreach inside a foreach. The first foreach seems to work fine and I don't get any php errors. However in each subsequent foreach loop I get a Message: Undefined property: stdClass::$filename error (replace filename with every single identifier). Can someone help me out and tell me what I'm doing wrong? , You have shown two ways of calling a resultset. I hope that is fine.
code :
foreach($query1->result() as $row1)
foreach($query2 as $row2)
Multiple PHP foreach loops on XML file retaining data from previous foreach loops

Multiple PHP foreach loops on XML file retaining data from previous foreach loops


By : Shookii
Date : March 29 2020, 07:55 AM
I hope this helps . Ok I solved it by just trying something random - I had created the new xml files using a foreach on an array of titles before any of the foreach loops to extract the xml data. However when I removed the loop to created the files and just put them separately before the loops to extract data the problem disappeared. It doesn't seem logical in my head, but that's how it is apparently!
Multiple foreach loops with arrays inside of an array

Multiple foreach loops with arrays inside of an array


By : Weegee126
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have an array that looks something like this: , You miss equal in value attribute.
code :
function setOptions($array){
    echo '<select name="select">';
    foreach($array as $key_parent => $val_parent){
        foreach($val_parent as $key => $val){
            if($key !== 'en'){
                $option_value = $val;
            }elseif($key == 'en'){
                $option_title = $val;
            }
        }
        echo '<option value="'.$option_value.'">'.$option_title.'</option>';
    }
    echo '<select>';
}
How to loop through multiple foreach loops and insert data into the database

How to loop through multiple foreach loops and insert data into the database


By : anurodh
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Assuming that your users array is indexed and your row data is an indexed array of associative arrays, you can marry-up/sync the data with a single loop using the shared index ($i).
code :
$users = User::all();
$rows = $data->toArray();
foreach ($users as $i => $user) {
    if (!$user->admin) {
        $Data[]= 
            [
            'user_id' => $user->id, 
            'avatar' => 'avatar.png',
            'about' => isset($rows[$i]['description']) ? $rows[$i]['description'] : 'default'
            ];
    }
}
profile::insert($Data); 
Multiple foreach loops inside each other

Multiple foreach loops inside each other


By : user2470587
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm not entirely sure what you're trying to do - you haven't given any example input and output, and your code is written in a very unclear style - but there's a number of things that look wrong to me.
code :
foreach ($files as $filename)
array_multisort(array_map('filectime', ($files = glob("*.*", GLOB_BRACE))), SORT_DESC, $files);
if (file_exists($filename))
$cam1 = ['a', 'b'];
$cam2 = ['c', 'd'];
$cam3 = ['e', 'f'];
$files = ['g', 'h'];
foreach ($cam1 as $cams1) {
     foreach ($cam2 as $cams2) {
          foreach ($cam3 as $cams3) {
              foreach ($files as $filename) {
                   echo "$cams1, $cams2, $cams3, $filename \n";
              }
          }
     }
}
a, c, e, g 
a, c, e, h 
a, c, f, g 
a, c, f, h 
a, d, e, g 
a, d, e, h 
a, d, f, g 
a, d, f, h 
b, c, e, g 
b, c, e, h 
b, c, f, g 
b, c, f, h 
b, d, e, g 
b, d, e, h 
b, d, f, g 
b, d, f, h 
if ($i == 13) break;
if ($i == 13) break 4;
Related Posts Related Posts :
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • Using same alias for multiple namespaces
  • how to move mails into separate folders
  • Multithreaded Uploader
  • Memory fragmentation?
  • C# correct exception handling
  • "Could not open macro storage" when accessing using file on another machine
  • How to access other directories of hosted server
  • C# Jagged Array check if value exists/true
  • Why can't I type Clone() properly?
  • exception on accessing dictionary from list
  • Getting the immediate response from server without waiting to 200 message
  • Why am I getting exception Directory Is Not empty?
  • Could not load file or assembly 'CefSharp.dll' or one of its dependencies
  • Sending Email By Using C# in unity3D?
  • Correct usage of await async in webapi
  • Program update code issue
  • Marshal.Copy attempted to read or write protected memory At Random Times
  • Restrict Type variable to specific class or subclass
  • Horizontal text alignment in a PdfPCell
  • C# crashing with Form.show() command, ObjectDisposedException - Deeper look / explanation please
  • Will the result of a LINQ query always be guaranteed to be in the correct order?
  • "Could not find default endpoint element that references contract"
  • Umbraco Request.QueryString is null if it's the first time the page is loaded
  • Error inconsistent accessibility method C#
  • How to program Intel Xeon Phi with C#?
  • remove nested element using regular expression
  • Is there a C# alternative to Java's vararg parameters?
  • Clear particular column values in DataTable
  • how to add event handler to programatically created checkboxes
  • Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection'
  • Check for key in pre-existing dictionary in case insensitive manner
  • How to remove year from datetime object?
  • Accessing Settings in different ways
  • "This project is empty" error in Sonarqube
  • How to create reusable icon menu in Xamarin
  • Value Cannot be null in Ado.Net connectivity
  • Adding a custom/dynamic attribute when using XSD.exe
  • How to convert object to correct type
  • Automatically sign out from Forms Authentication in ASP.NET when browser is closed
  • Can a WCF service support both Buffered and Streamed transfer modes?
  • Verify a CA Certificate with a public key in C#
  • How to invoke a Web Service that requires the "patch" verb using the C# WebClient wrapper?
  • Proper way a implementing property based on generic type
  • Closing a form that is created in another thread
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org