dreamwind1985 2016-10-20 13:19
浏览 32
已采纳

检查数组是否包含与正在迭代的另一个数组相同的键和值。 PHP

First of all , the framework I'm using is Laravel 5.3

What I'm trying to accomplish is to find a way to minimize query in my application.I'm working on a restaurant web control panel , on the view wich I'm supposed to edit the list of dishes form respective categories assigned to a daily special menu.A preview of the view is shown below :

enter image description here

I have two queries that after some work done on their results, generate each a different array.

The first array named : $selectable_categories_and_dishes returns an array of all the categories and dishes in the database(This includes the categories and respective dishes that are already selected).

Array's objects and structure :

{
  "id_category":4,
  "category_name":"Contorno ",
  "dishes":[
            {
             "id":15,
             "price":"10",
             "name":"Patate al Forno"
            },
            {
             "id":16,
             "price":
             "8","name":"Verdure alla Griglia"
            },
            {
             "id":17,
             "price":"8",
             "name":"Insalata Mista"
            }
          ],
}

The second array named : $selected_categories_and_dishes returns an array of all the categories and dishes that were selected when the daily special menu was created in the first place . Array's objects and structure :

{
  "id_category":4,
  "category_name":"Contorno ",
  "dishes":[
            {
             "id":15,
             "price":"10",
             "name":"Patate al Forno",
             "id_pcm":"4"
            },
            {
             "id":16,
             "price":"8",
             "name":"Verdure alla Griglia",
             "id_pcm":"4"
            },
            {
             "id":17,
             "price":"8",
             "name":"Insalata Mista",
             "id_pcm":"4"
            }
          ],
}

What I can't figure out is a way to merge these arrays together in a way that when I loop the merged array , all the selected dishes are checked.

Does any one have any idea how to accomplish that.Any response is appreciated. Thanks in advance.

Edit:

The way I generate this arrays id as follows :

 $menu_categories = DB::select('SELECT id , name FROM `packages_menu_categories` WHERE visible = "yes" AND `delete` = "no"');
        $selectable_categories_and_dishes = array();
        foreach ($menu_categories as $menu_category)
        {
            $dishes =  DB::select('SELECT d.id , d.price , d.name FROM `package_menu_categories_dishes` p LEFT JOIN `dishes` d ON p.id_dish = d.id WHERE p.visible = "yes" AND p.delete = "no" AND p.id_package_menu_category ='.$menu_category->id);
            $object=
                [
                    'id_category' => $menu_category->id,
                    'category_name' =>$menu_category->name,
                    'dishes' => $dishes
                ];

            array_push($selectable_categories_and_dishes,$object);
        }

        $selected_categories = DB::select('SELECT c.id as id_category , c.name as category_name FROM sub_packages_dishes s LEFT JOIN packages_menu_categories c ON s.id_package_menu_category = c.id WHERE s.id_sub_package = '.$id.' GROUP BY s.id_package_menu_category');
        $dishes_and_categories  = array();
        foreach ($selected_categories as $selected_category)
        {
            $dishes =  DB::select('SELECT s.id AS id_pmcd , d.id , d.name FROM `sub_packages_dishes` s LEFT JOIN `dishes` d ON s.id_dish = d.id WHERE s.id_sub_package ='.$id.' AND s.id_package_menu_category ='.$selected_category->id_category);
            $object=
                [
                    'id_category' => $selected_category->id_category,
                    'category_name' =>$selected_category->category_name,
                    'dishes' => $dishes
                ];
            array_push($dishes_and_categories,$object);
        }
  • 写回答

1条回答 默认 最新

  • dplsnw7329 2016-10-20 14:35
    关注

    My suggestion assuming you can control the array's structure is make dishes an associative array and the key as the dish id. Then as you loop check that the one you are on exist in the selected

    Essentially this would be a structure:

    {
    "id_category":4,
    "category_name":"Contorno ",
    "dishes":{
        15 => {
            "price":"10",
            "name":"Patate al Forno",
            "id_pcm":"4"
        },
        16 => {
            "price":"8",
            "name":"Verdure alla Griglia",
            "id_pcm":"4"
        }
        ...
    }
    

    Then as you loop your array it is as simple as. This may not be 100% but gets the point across.

    foreach($selectable_categories_and_dishes as $key => $values){
        if(isset($selected_categories['dishes'][$key])){
            //This is selected so check the box. 
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料