在foreach循环外访问变量PHP

Swe*_*tha 5 php variables foreach magento

我是PHP新手。谁能告诉我如何在foreach之外访问foreach循环变量。请按代码查找以下内容。

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

我在以下html img标签的src属性中需要它。

<div class="SizeChat"><p>close</p><div class="Padd"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$categorySize ?>"></div></div>
Run Code Online (Sandbox Code Playgroud)

Man*_*mer 5

首先定义循环上方的变量

$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 
Run Code Online (Sandbox Code Playgroud)