How to randomly iterate through an array once, and then repeatedly iterate in that order
By : dreikelvin
Date : March 29 2020, 07:55 AM
will help you Create a second array. Next, populate the array with random numbers (ranging from 0 to array.length). Now create a for loop iterating through the secondary array. Each number in the array corresponds to an index in array. Result: you can now iterate randomly through array without modifying the order of array.
|
Iterate over array in reverse order
By : Ricardo Duarte
Date : March 29 2020, 07:55 AM
will help you I have the following loop - , Since counts is an object, try this way. code :
for (var i = Object.keys(counts).length; i > 0; --i) {
}
|
How to iterate thru array with asynchronous method, and put return values into an array in the PROPER order
By : sky.z
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Check if this works, declaring a fixed length array and accessing the array element by index: code :
func getValues(valueIDs: [Int]){
var items = [Item?](count: valueIDs.count, repeatedValue: nil)
let group = dispatch_group_create()
for i in 0...valueIDs.count-1 {
dispatch_group_enter(group)
Item.special(valueIDs[i], completion: ({ result in
if let value = result.response.result {
items[i] = value
dispatch_group_leave(group)
}
})
)
}
dispatch_group_notify(group, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
print("the values are \(items)")
}
}
|
PHP Order Variables By Array Count Iterate Multi-Dimensional Array
By : Arnold
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You can use a foreach() loop and iterate over each of the 3 arrays and use fputcsv() to save the 3 items into a CSV file. code :
$fp = fopen('file.csv', 'w');
$year = array('1999','2000','2001','2002');
$fruit = array('apple','orange','strawberry');
$color = array('red','green');
foreach ($year as $y) {
foreach ($fruit as $f) {
foreach($color as $c) {
echo "$y,$f,$c" . PHP_EOL; // Echo to screen. Not needed
fputcsv($fp,array($y,$f,$c)); // Save each row to CSV file
}
}
}
fclose($fp);
|
Recursive array search - keep proper order and iterate down array
By : Ian
Date : March 29 2020, 07:55 AM
Any of those help A tree structure is not really helpful for this purpose. You should be thinking about how to create a data structure that makes it easy for you to match the input. Since your category input describes a branch of the tree, the best thing to do is build an array that you can use to match those branch descriptions to your categories efficiently. Let's build an array where the keys are the paths for each category as described by their slugs, and the values are the category IDs. We can then immediately identify the matching category, or fail if the path is not in the array. code :
<?php
//Mock category records, would come from the DB in the real world
$categoryRecords = [
['id' => 1, 'title' => 'Radios', 'slug' => 'radios', 'parent_id' => 0],
['id' => 2, 'title' => 'Accessories', 'slug' => 'misc', 'parent_id' => 1],
['id' => 3, 'title' => 'Motorola', 'slug' => 'motorola', 'parent_id' => 1],
['id' => 4, 'title' => 'Handheld', 'slug' => 'handheld', 'parent_id' => 3],
['id' => 5, 'title' => 'Mobile', 'slug' => 'mobile', 'parent_id' => 3]
];
//Create an array that maps parent IDs to primary keys
$idMap = [];
foreach ($categoryRecords as $currRecord)
{
$idMap[$currRecord['id']] = $currRecord;
}
//Traverse the flat array and build the path lines
$paths = [];
$categoryIds = array_keys($idMap);
foreach ($categoryIds as $currLeafId)
{
$currCategoryId = $currLeafId;
$currLine = [];
do
{
$currLine[] = $idMap[$currCategoryId]['slug'];
$currCategoryId = $idMap[$currCategoryId]['parent_id'];
} while ($currCategoryId != 0);
$currLine = array_reverse($currLine);
$currPath = implode('/', $currLine);
$paths[$currPath] = $currLeafId;
}
//Join your input - $_GET['var'] in your example
$inputPath = implode('/', ['radios', 'motorola', 'handheld']);
//Now you can see if the incoming path matched a category
if(array_key_exists($inputPath, $paths))
{
$category = $categoryRecords[$paths[$inputPath]];
echo 'Matched category: '.$category['title'].PHP_EOL;
}
else
{
echo 'Invalid category path';
}
|