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
Codeigniter/PHP:如何合并不同的数组并使其在查询结果中成为唯一的数组_Php_Mysql_Arrays_Codeigniter - Fatal编程技术网

Codeigniter/PHP:如何合并不同的数组并使其在查询结果中成为唯一的数组

Codeigniter/PHP:如何合并不同的数组并使其在查询结果中成为唯一的数组,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,-成分 -菜单 配方 如何将name下的两个数组合并为一个数组并使其唯一。正如您所看到的,[0]上的结果是 牛肉阿多博 qwerty 伊斯维 还有就是 qwerty 伊斯维 我希望它们都在一个数组中,结果应该是 牛肉阿多博 qwerty 伊斯维 查询: public function get_halal($name) { $terms = explode(',', $name); foreach ($terms as $name) { $this->d

-成分

-菜单 配方

如何将name下的两个数组合并为一个数组并使其唯一。正如您所看到的,[0]上的结果是

牛肉阿多博

qwerty

伊斯维

还有就是

qwerty

伊斯维

我希望它们都在一个数组中,结果应该是

牛肉阿多博

qwerty

伊斯维

查询:

public function get_halal($name) {

    $terms = explode(',', $name);

    foreach ($terms as $name) {
        $this->db->distinct();
        $this->db->select('r_name');

        $this->db->from('recipe');

        $this->db->join('menu', 'menu.recipe_id = recipe.recipe_id');
        $this->db->join('ingredient', 'ingredient.ingredient_id = menu.ingredient_id');
        $this->db->where('menu.category_id = 2');
        $this->db->like('ingredient.name', $name);

        $query = $this->db->get()->result();

        $data[] = $query;
    }
    return $data;
}
public function ajaxSearchHalal() {
    postdata = file_get_contents("php://input");

    if (isset($postdata)) {
        $post = json_decode($postdata);

        $name = $post->name;


        if ($this->user_model->get_halal($name)) {
            $user_id = $this->user_model->get_halal($name);

            $data = array(
                'name' => $user_id,
            );

            echo json_encode($data);
        } else {
            echo json_encode("false");
        }


    } else {
        echo "Error!";
    }
}
控制器:

public function get_halal($name) {

    $terms = explode(',', $name);

    foreach ($terms as $name) {
        $this->db->distinct();
        $this->db->select('r_name');

        $this->db->from('recipe');

        $this->db->join('menu', 'menu.recipe_id = recipe.recipe_id');
        $this->db->join('ingredient', 'ingredient.ingredient_id = menu.ingredient_id');
        $this->db->where('menu.category_id = 2');
        $this->db->like('ingredient.name', $name);

        $query = $this->db->get()->result();

        $data[] = $query;
    }
    return $data;
}
public function ajaxSearchHalal() {
    postdata = file_get_contents("php://input");

    if (isset($postdata)) {
        $post = json_decode($postdata);

        $name = $post->name;


        if ($this->user_model->get_halal($name)) {
            $user_id = $this->user_model->get_halal($name);

            $data = array(
                'name' => $user_id,
            );

            echo json_encode($data);
        } else {
            echo json_encode("false");
        }


    } else {
        echo "Error!";
    }
}

好的,我知道你现在在做什么了。谢谢你的桌子

据我所见,您正试图从
recipe
表中获取包含您正在传递的成分的所有配方名称

要解决这个问题,您需要做的不是担心如何合并数组,而是如何重做sql查询,以便在一个查询中获得所需的信息。现在的方式效率不高,因为每次迭代都要调用一个查询

您需要做的是使用
WHERE IN
groupby
来获取所需信息并按列进行分组。重做模型方法,如下所示:

public function get_halal($name) {

    $terms = explode(',', $name);

    $this->db->select("r.name");
    $this->db->from('recipe r');
    $this->db->join('menu m', 'm.recipe_id = r.recipe_id');
    $this->db->join('ingredient i', 'i.ingredient_id = m.ingredient_id');
    $this->db->where('m.category_id = 2');
    $this->db->where_in('i.name', $terms);
    $this->db->group_by('r.recipe_id');
    $q = $this->db->get();
    return $q->result();
}
这将为您提供一个结果集,然后您可以将其作为
JSON
传递到前端,而无需迭代或合并数组


希望这有帮助。

您的
菜单
表的确切用途是什么?您能在数据库中显示一个
菜单
表的示例吗?我询问
菜单
表的原因是,您不必担心合并结果数组。您只需要修复您的查询。为您的表格提供示例,以便我们能给您最好的答案。@CodeGodie菜单表格是我可以获取我的食谱类别id的地方。没问题。很高兴我能帮忙。