How to Get Foreach Loop Variable Outside of Loop

Access variable outside foreach loop PHP

First define the variable above of the loop

$categorySize = array();

<?php $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');
foreach ($categories as $category):
$categorySize = $category->getSize_chart();
print_r ($categorySize);
endforeach;
?>

print_r($categorySize) //Now you can get it outside the loop

how can i get foreach loop variable outside of loop

You have just to append your variable like this

$pages = get_pages();
$pagee = array();
foreach ($pages as $page) {
$pagee[] = $page->post_title;
}
echo implode(",",$pagee);

How to make variable global outside foreach loop in php?

You need to define the variable outside loop if you want to access it outside as well.

$folder = $obj->get_all_folders();        
$existing_directory = null;
foreach ($folder as $folder_data)
{
$existing_directory = $folder_data['root_dir'];
}

as per your code you will get last record from array
so you can try below code as well

$folder = $obj->get_all_folders();        
$last_record = end($folder);
$existing_directory = $last_record['root_dir'];

Can't acess variable outside forEach loop JavaScript

Async functions don't do what you think they do in forEach. Foreach will wait for every loop, but since it won't return a Promise, you can't actually wait for all loops (the forEach) to be done - the method will just return the current null immediately. Then you log that once. You might notice that your last log is logged first, and your others later, indicating that forEach was completed, but the loops completed later, and your array only filled up after your first log.

Personally, I would suggest using a for loop itself instead, which does not actually take you out of the async flow:

const allItems = [];   

for( const element of orderPads ){
for( const elem of element.orders_fk ){

const itemsArray = await OrderedItem.find({ order_fk: elem });

for( const e of itemsArray ){

const temp = e.toObject();
const mesa = await Table.findById(element.mesa);
temp.mesa = mesa.name;

allItems.push(temp)
console.log(allItems)

}

}}

console.log(allItems)

How do I access a variable outside of an foreach (C#)

I cannot understand what do you want to achieve... but the code should be this:

 ArrayList lines = GetLines("test.txt", "8394", true);
string result=string.Empty;
foreach (string s in lines)
{
result = s;
}
Console.WriteLine(result);

I think you want do to something like this:

 ArrayList lines = GetLines("test.txt", "8394", true);

foreach (string s in lines)
{
Console.WriteLine(s);
}

Variable outside foreach loop

$date_array = array();
if( !empty($row_edit) ){
$stageDates = explode(', ',$row_edit['placement_stage_date']);
foreach( $stageDates as $stageDate ){
//echo $stageDate.'<br/>';
$date_array[] = $stageDate;
}
}
// print $stageDate value outside of loop, something like this

<input type="text" name="placement_stage_date[]" value="<?php echo $date_array[0]; ?>" />
<input type="text" name="placement_stage_date[]" value="<?php echo $date_array[1]; ?>" />
<input type="text" name="placement_stage_date[]" value="<?php echo $date_array[2]; ?>" />


Related Topics



Leave a reply



Submit