Magento-我可以对整个产品系列进行JSON编码吗?

问题描述

| 到目前为止,这就是我所拥有的。看起来很亲密吗?还是有更简单的方法
protected function _encodedProductCollection()
{
    $productcollection = $this->_getProductCollection();
    $productmodel = Mage::getModel(\'catalog/product\');
    $categorymodel = Mage::getModel(\'catalog/category\');

    $collection_to_encode = array();

    foreach( $productcollection as $product )
    {
        $product_to_encode = array();

        $thisproduct = $productmodel->load($product->getId());
        $productsname = $thisproduct->getName();
        $productsid = $thisproduct->getId();
        $productcategories = $thisproduct->getCategoryIds();
        $productscategoryname = $categorymodel->load($productcategories[0])->getName();  //we\'ll start with the first category name.
        //$productsimageurl = $thisproduct->getimageUrl();  //gets the image url if we need it later
        //$altcategorymethod = ($thisproduct->getCategory() ? $thisproduct->getCategory()->getName() : \'No Category\');  //we may use this one instead.



        $product_to_encode[] = array(
            \'id\' => $productsid,\'name\' => $productsname,\'category\' => $productscategoryname );

        array_merge($product_to_encode,$collection_to_encode); 
    }

    $encoded_products = json_encode($collection_to_encode);

    return $encoded_products;  //leave this if we leave it as a function - we may port it all over to the template itself.
}
    

解决方法

        那个部分:   array_merge($ product_to_encode,$ collection_to_encode); 将不起作用,因为array_merge返回合并的数组。在您的情况下,合并数组不会被变量捕获。 以下部分应使其起作用:   $ collection_to_encode = array_merge($ product_to_encode,$ collection_to_encode);