如何使用PHP组合并删除重复的数组值?

时间:2014-12-17 02:18:16

标签: php arrays

我的代码有问题。我正在我的页面中创建一个功能,可以获得基于类别的推荐产品。但我的数组按类别分隔。我想要的是将所有产品组合成一个大的阵列。我应该验证是否存在现有ID。

到目前为止,这是我的代码中的内容: 顺便说一句,我正在使用MVC方法。

MODEL

public function suggestProduct($category_id) {

    $query = $this->db->query("SELECT product_id, category_id FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$category_id . "'");
    $get_products = $query->rows;

    $products = array();

    foreach($get_products as $product) {

        //echo $product['product_id']."<br />";

        if(!in_array($product['product_id'], $products)) {

            $products[] = $this->getProduct($product['product_id']);

        }

    }

    return $products;

}

我使用in_array函数删除重复的ID。

CONTROLLER

$data['recommended_products'] = array();

foreach($get_categories as $category) {

    $data['recommended_products'][] = $this->model_catalog_product->suggestProduct($category);

}

fp($data['recommended_products']); //PRINT OUTPUT

在我的输出中,我有这个。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [product_id] => 41
                    [category_id] => 27
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [product_id] => 28
                    [category_id] => 20
                )

            [1] => Array
                (
                    [product_id] => 29
                    [category_id] => 20
                )

            [2] => Array
                (
                    [product_id] => 30
                    [category_id] => 20
                )

            [3] => Array
                (
                    [product_id] => 33
                    [category_id] => 20
                )

            [4] => Array
                (
                    [product_id] => 35
                    [category_id] => 20
                )

            [5] => Array
                (
                    [product_id] => 40
                    [category_id] => 20
                )

            [6] => Array
                (
                    [product_id] => 42
                    [category_id] => 20
                )

            [7] => Array
                (
                    [product_id] => 43
                    [category_id] => 20
                )

            [8] => Array
                (
                    [product_id] => 44
                    [category_id] => 20
                )

            [9] => Array
                (
                    [product_id] => 46
                    [category_id] => 20
                )

            [10] => Array
                (
                    [product_id] => 47
                    [category_id] => 20
                )

            [11] => Array
                (
                    [product_id] => 48
                    [category_id] => 20
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [product_id] => 43
                    [category_id] => 18
                )

            [1] => Array
                (
                    [product_id] => 44
                    [category_id] => 18
                )

            [2] => Array
                (
                    [product_id] => 45
                    [category_id] => 18
                )

            [3] => Array
                (
                    [product_id] => 46
                    [category_id] => 18
                )

            [4] => Array
                (
                    [product_id] => 47
                    [category_id] => 18
                )

        )

)

但我的in_array函数只对当前数组有效。如何在所有阵列中执行验证?

1 个答案:

答案 0 :(得分:2)

由于$get_categories是ids的集合,因此您只需使用IN子句:

public function suggestProduct($category_id) 
{

    $category_ids = implode(',', array_unique($category_id));
    $query = $this->db->query("
        SELECT product_id, category_id FROM " . DB_PREFIX . "product_to_category 
        WHERE category_id IN($category_ids)
    ");

    $get_products = $query->rows;

    $products = array();

    foreach($get_products as $product) {

        //echo $product['product_id']."<br />";

        if(!in_array($product['product_id'], $products)) {

            $products[] = $this->getProduct($product['product_id']);

        }

    }

    return $products;
}

然后在控制器内:

$data['recommended_products'] = $this->model_catalog_product->suggestProduct($get_categories);