Checking if array contains the same key and value as another array wich is being iterated. PHP

时间:2016-10-20 13:19:46

标签: php arrays laravel-5.3 arrayobject

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 个答案:

答案 0 :(得分:1)

我的建议假设您可以控制数组的结构,将菜肴作为关联数组,将关键字作为菜单ID。然后循环检查您所在的那个是否存在于所选的

基本上这将是一个结构:

{
"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"
    }
    ...
}

然后当你循环你的数组时,就像它一样简单。这可能不是百分之百,但可以解决问题。

foreach($selectable_categories_and_dishes as $key => $values){
    if(isset($selected_categories['dishes'][$key])){
        //This is selected so check the box. 
    }
}