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 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能