2013-12-13 34 views
0

我正在使用基于mcv设计模式的OpenCart商店。我需要一个模型函数返回一个数组,但我需要在该数组内调用另一个返回数组的函数。下面这个函数打破了这里:PHP - 将数组返回给另一个数组的组合函数

'gallery_images' => $this->getGalleryImages($data['product_id'])

public function getImportProductInfo() { 
    $query = $this->db->query("SELECT `product_id` FROM " . DB_PREFIX . "product WHERE `status` = '0' AND `affiliate_id` = '" . $this->affiliate->getID() . "'"); 

    $pids = array(); 
    foreach($query->rows as $result) { 
     $pids[] = $result['product_id']; 
    } 

    // product & product description 
    $query_product = $this->db->query("SELECT p.model, 
               p.product_id, 
               p.quantity, 
               p.image, 
               p.price, 
               p.weight, 
               p.length, 
               p.width, 
               p.height, 
               pd.name AS 'product_name', 
               pd.description AS 'product_description', 
               cd.name AS `category_name`, 
               m.name AS 'manufacturer_name' 
             FROM  " . DB_PREFIX . "product p 
             LEFT JOIN " . DB_PREFIX . "product_description pd ON p.product_id = pd.product_id 
             LEFT JOIN " . DB_PREFIX . "product_to_category ptc ON p.product_id = ptc.product_id 
             LEFT JOIN " . DB_PREFIX . "category_description cd ON ptc.category_id = cd.category_id 
             LEFT JOIN " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id 
             WHERE  p.product_id IN (" . $this->db->escape(implode(',',$pids)) . ")"); 


    foreach($query_product->rows as $data) { 
      $product_data[] = array(
       'product_id'   => $data['product_id'], 
       'model'    => $data['model'], 
       'quantity'    => $data['quantity'], 
       'featured_image'  => $data['image'], 
       'price'    => $data['price'], 
       'weight'    => $data['weight'], 
       'length'    => $data['length'], 
       'width'    => $data['width'], 
       'height'    => $data['height'], 
       'product_name'   => $data['product_name'], 
       'description'   => $data['product_description'], 
       'category_name'  => $data['category_name'], 
       'manufacturer_name' => $data['manufacturer_name'], 
       'gallery_images'  => array($this->getGalleryImages($data['product_id'])) 

      );  
    } 

    return $product_data; 
} 

这里是getGalleryImages()函数。注意这里我也试过return implode(', ',$images);,所以我没有建立数组,但它仍然打破。

public function getProductGalleryImages($product_id) { 
    $query = $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$product_id . "'"); 

    $images = array(); 
    foreach($query->rows as $result) { 
     $images[] = $result['image']; 
    } 

    //return implode(', ',$images); 
     return $images; 
} 

每个产品都有存储在交叉表中的多个图像的URL,我需要添加到阵列,它一个上午都在战斗我...任何想法将不胜感激。在此先感谢:)这里

OK是嵌入在阵列中的SQL工作

'gallery_images' => $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$data['product_id'] . "'") 

但它返回这样的:

[gallery_images] => stdClass Object ( 
[row] => Array ([image] => motorcycle/12_06_27/031.JPG) 
    [rows] => Array ([0] => Array ([image] => motorcycle/12_06_27/031.JPG))[num_rows] => 1))) 

所有我想要的是['image']一部分。

回答

3

只需省略“数组”部分即可。取而代之的

array($this->getGalleryImages($data['product_id'])) 

使用

$this->getGalleryImages($data['product_id']) 

因为(如果)这个函数会返回一个数组,这是正确的语法。 如果你想成为安全有关的返回值是一个数组(即,强制它是),你可以这样做:

array() $this->getGalleryImages($data['product_id']) 

......这几乎是同样的事情,但是请注意,数组()括号立即关闭。这意味着你“结果”到数组中。 (我不知道这是你的初衷btw ...吗?)

你可以在数组中使用数组,这是没有问题的。现在,如果这不是正确的答案,让我们澄清这个问题本身:)

+0

@dkelliner所以,你的答案是正确的我的问题,但我已经尝试了所有这些情况下,他们仍然打破这就是为什么我感到困惑。如果我直接在数组中嵌入sql,它会工作,但它返回一堆我不需要的东西。我已经添加到上面的代码来显示我的结果。 – handmdmr

+0

告诉我它不是“getProductGalleryImages”vs“getGalleryImages”:) – dkellner

+0

(另外,你正在查询里面的查询对你的速度来说不是很健康,还有其他方法,但这是另外一个问题。) – dkellner

相关问题