foreach with in a foreach php


foreach with in a foreach php

嗨,我有一个脚本,其中foreach中有foreach第一个foreach用于唯一的id,但第二个foreach用于产品的相关图像,应根据图像数量进行循环。但是,它限制我只能获取一个相关图像,如何获取所有相关图像?

$i=0;
foreach ($collection as $product_all) { 
    //echo $product_all->getId().'<br/>';
    if($i==10) break;  
    $id = $product_all->getId();        
    $neew = Mage::getModel('catalog/product')->load($id);         
    //echo'<pre>';
        echo 'active products id ===='.$neew ->getId().'<br/>';  
        echo 'active products name ===='.$neew->getname().'<br/>';    
        echo 'active products style_ideas ===='.$neew->getstyle_ideas().'<br/>';  
        echo 'active products size and fit ===='.$neew->getsize_fit().'<br/>';  
        echo 'active products short description ===='.$neew->getshort_description().'<br/>';  
        echo 'active products  description ===='.$neew->getdescription().'<br/>';  
        //print_r($neew); 
        if (count($neew->getMediaGalleryImages()) > 0){
            $i = 0 ;
            $j = 0 ;
            foreach ($neew->getMediaGalleryImages() as $_image){
                  $relative_image = Mage::helper('catalog/image')->init($neew->getProduct(), 'image', $_image->getFile())->resize(2000);
                  $relative_image1 = str_replace('/webApps/migration/productapi/new/','/',$relative_image);
                  //echo 'relative_image => '.$relative_image1.'<br/>';
                  $relative_image_save = $relative_image1 ;
                  $relative_image_save1 = explode('/', $relative_image_save);//explode / to get only image name
                  $end1 = end($relative_image_save1);
                  $relative_image3 = $end1;
                  //$handle1 = fopen( $relative_image3, 'w') or die('Cannot open file:  '. $relative_image);
                  $path12 = '/mnt/aviesta/development/webApps/migration/productapi/new/'.'sku-'.$neew->getsku().'_' .$i++.'.jpg';
                  copy($relative_image_save, $path12);
                  echo 'relative image with sku path_'.$j++.' ====>'.$path12.'<br/><br/>'; 
               }
           }
      $i++;
}

从您的代码中,我认为可以通过在第二个 foreach 循环之前初始化一个数组来解决这个问题,并且将该数组中的图像存储在该数组中可以帮助您获取所有图像......这是我从你的问题中理解的。如果这是正确的..,那么下面的代码可能会起作用。

foreach()
{
    //Your code....
     $image_list = array();
     $i = 0;
     foreach()
      {
        // Your usual code 
       $image_list[$i] = your_image;
       //storing images one-by-one in your variable..
       $i++;
       }
}

您可以稍后返回变量或仅返回 print_r(); 来获取输出

我想

我已经匆地发布了这个问题,脚本工作正常,对于初始产品,产品只有一个图像,但对于较新的产品,有多个图像,脚本工作正常,感谢大家回复帖子.....