在foreach循环外访问变量PHP [英] Access variable outside foreach loop PHP

查看:88
本文介绍了在foreach循环外访问变量PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to PHP. Can anyone tell me how to access the foreach loop variable outside foreach. Please find below by code.

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

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

I need it in the following html img tag's src attribute.

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

推荐答案

首先在循环上方定义变量

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 

这篇关于在foreach循环外访问变量PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆