0

嗨,我有一个从magento获取所有产品数据的脚本,出现了一个问题,即有些产品的名称相同不同我想附加具有相同值的产品名称,而其他具有唯一值的产品名称不应该附加...

<?php
@ob_start();
@session_start();
ini_set('display_errors', 1);
include '../../../../app/Mage.php';
umask(0);
Mage::app('default');

function empty_pk($data){
if($data!=''){return $data;}
else {return "&nbsp;";}
}
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status', 1);
//arsort($collection);  

//echo'<pre>';
 //print_r($collection);die();
echo 'count===='.count($collection).'<br/>';
$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>';

$product_id = $neew->getId();
$created_at = ' 2013-01-26 00:53:46';
$description = $neew->getdescription();
$short_description = $neew->getshort_description();
$sku = $neew->getsku();
$size_fit = $neew->getsize_fit();
$style_ideas = $neew->getstyle_ideas();
$name = $neew->getname();

我怎样才能做到这一点

4

1 回答 1

0

您可以使用临时数组来保存名称的计数,然后按它进行过滤:

function empty_pk($data){
if($data!=''){return $data;}
else {return "&nbsp;";}
}
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status', 1);
//arsort($collection);  

$names = array();
foreach ($collection as $product_all) {
  $name = $product_all->getName();
  if (array_key_exists($name)) {
      $names[$name]++;
  } else {
      $names[$name] = 1;
  }
}
//echo'<pre>';
 //print_r($collection);die();
echo 'count===='.count($collection).'<br/>';
$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>';

// Exit if unique.
if ($names[ $neew->getName() ] == 1) {
    break;
}

$product_id = $neew->getId();
$created_at = ' 2013-01-26 00:53:46';
于 2013-01-28T09:17:24.817 回答