Search code examples
phparrayslaravel-5arrayobject

Checking if array contains the same key and value as another array which is being iterated. 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 which 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.

How can I do that?

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

Solution

  • 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. 
        }
    }