Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP组合并删除重复的数组值?_Php_Arrays - Fatal编程技术网

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

如何使用PHP组合并删除重复的数组值?,php,arrays,Php,Arrays,我的代码有问题。我正在我的页面中创建一个功能,可以根据类别获得推荐产品。但是我的数组是按类别分开的。我想要的是将所有产品组合成一个大阵列。我应该验证是否存在现有ID 到目前为止,我的代码中包含以下内容: 顺便说一下,我正在使用MVC方法 型号 public function suggestProduct($category_id) { $query = $this->db->query("SELECT product_id, category_id FROM " . DB_

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

到目前为止,我的代码中包含以下内容: 顺便说一下,我正在使用MVC方法

型号

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;

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

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_数组函数仅在当前数组中有效。如何在所有阵列中执行验证

由于
$get_categories
是一组ID,因此您可以在
子句中使用

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