Wednesday, 29 August 2018

Create a multidimensional array in PHP with dynamic data

I am preparing a menu system for a restaurant which provides food on a monthly basis. This is my problem:

There are different packages that the restaurant offers. Each package consists of a number of servings each day. For example, package A serves 3 times a day, whereas package B serves 2 times a day. The online ordering system that I am building is a multi-page ordering system divided as per the number of days. So for 20 days, there are 20 pages. Once the selection of one day is completed, I want to store the selection in a multi-dimensional array. Refer to the below structure for reference.
$selection_package_a = array(
   "Serving_Day1" => array (
      "Serving_1" => Pizza,
      "Serving_2" => Salad,
      "Serving_3" => Smoothies
   ),
   "Serving_Day2" => array (
      "Serving_1" => Salad,
      "Serving_2" => Juices,
      "Serving_3" => Fruits
   ),
);

$selection_package_b = array(
   "Serving_Day1" => array (
      "Serving_1" => Pizza,
      "Serving_2" => Salad
   ),
   "Serving_Day2" => array (
      "Serving_1" => Salad,
      "Serving_2" => Juices
   ),
);

"Serving_Day1" to "Serving_Day20" depends on the number of days served during a month. So if the package serves only 10 days a month, then "Serving_Day10" will be the last field.
Within "Serving_Day1", "Serving_1" and so on depends on the number of servings stored in the database.
Taking the answer of @yarwest a step forward, I have pasted the progress till now. I guess it is just one more step to acheive the desired output.
$meals_selected_array = [];

$total_meals_array = [];

if( $num_row_packages >= 1 ) {
    while($row_packages = mysqli_fetch_array ($result_packages)) {
        $package_id = $row_packages['package_id'];
        $package_name = $row_packages['package_name'];
        $servings_count = $row_packages['servings_count'];
        $days_served = $row_packages['days_served'];

        //repeating it based on the number of days_served
        for ($i = 1; $i <= $days_served; $i++) {
            //how to define/declare $total_meals_array['day_' . $i]            

            //adding user selection for the day in $meals_selected_array array
            for ($y = 1; $y <= $servings_count; $y++) {
                $meals_selected_array["meal_id_day_" .$i] = "Not Available";
                $meals_selected_array["meal_code_day_" .$i] = "Not Available";
                $meals_selected_array["meal_type_day_" .$i] = "Meal";
            }

            //what to do either here or after the below loop in order to add $meals_selected_array above values to $total_meals_array['day_' . $i].
        }
    }
}

When I $print_r($meals_selected_array), I get the result as an Associative Array with perfect labelling and values. Now I just have to add this Associative Array to each day to make my primary Array as a Multidimensional Array.
So my desired output for $total_meals_array is as below:
Array
(
    [day_1] => Array
        (
            [meal_id_day_1] => "1" //This will be my Unique ID of selected meal
            [meal_code_day_1] => "Pizza" //This will be the name of meal
            [meal_type_day_1] => "Main Course" //This will be the serving Type
        )

    [day_2] => Array
        (
            [meal_id_day_2] => "4" //This will be my Unique ID of selected meal
            [meal_code_day_2] => "Lemonade" //This will be the name of meal
            [meal_type_day_2] => "Drinks" //This will be the serving Type
        )

    [day_3] => Array
        (
            [meal_id_day_3] => "8" //This will be my Unique ID of selected meal
            [meal_code_day_3] => "Custard" //This will be the name of meal
            [meal_type_day_3] => "Dessert" //This will be the serving Type
        )

)


Important note beforehand

It is very unsafe to use mysqli functions. They can easily be manipulated with, for example, SQL injections. Instead use PDO and Prepared Statements.

The solution

You retrieved a package from the database. The next step is to loop based on the amount of servings and amount of days to create a new array to hold the package.
if( $num_row_packages >= 1 ) {
    while($row_packages = mysqli_fetch_array ($result_packages)) {
        $package_id = $row_packages['package_id'];
        $package_name = $row_packages['package_name'];
        $servings_count = $row_packages['servings_count'];
        $days_served = $row_packages['days_served'];

        //Create a new array to hold the servings skeleton
        $servingsArray = [];
        //For every serving add an empty string to the servingArray
        for($i = 0; $i < $servings_count; $i++){
            array_push(
                $servingsArray,
                ""
            );
        }

        //Create a new array to hold the package skeleton
        $selection_package = [];
        //For every day add an servingArray to the package array
        for($i = 0; $i < $days_served; $i++){
            array_push(
                $selection_package,
                $servingsArray
            );
        }
    }
}

This will create an entirely empty array according to the specified structure.

0 comments:

Post a Comment