PHP - 将返回数组的函数组合到另一个数组中

时间:2013-12-13 16:46:28

标签: php arrays

我正在使用基于mcv设计模式的OpenCart商店。我需要一个返回数组的模型函数,但我需要在该数组中调用另一个返回数组的函数。下面的这个功能在这里打破:

'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;
}

每个产品都有多个图像网址存储在交叉表中,我需要添加到数组中,它一直在打扰我整个上午...任何想法都将非常感激。在此先感谢:)

好的,这里是嵌入在数组中的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']部分。

1 个答案:

答案 0 :(得分:3)

省略“数组”部分。而不是

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

使用

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

由于(以及如果)此函数将返回一个数组,这是正确的语法。 如果你想确保返回值是一个数组(也就是强制它),你可以这样做:

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

...这几乎是相同的,但请注意,array()括号会立即关闭。这意味着您将结果“强制转换”为数组。 (我想知道这是不是你的初衷......?)

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