Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP |如何定义大型空多维数组/对象_Php_Arrays_Multidimensional Array - Fatal编程技术网

PHP |如何定义大型空多维数组/对象

PHP |如何定义大型空多维数组/对象,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我试图在MongoDB中存储用户对详细健身报告问卷的响应。提交表单后,我将解析表单数据并创建一个具有预定义结构的数组/对象 我正在考虑创建一个具有整个对象层次结构的类,其值设置为空字符串“”。我要存储的响应如下所示: { "_basic": { "_response_type": "text", "_response": { "name": "eNeMetchH", "gender": "male",

我试图在MongoDB中存储用户对详细健身报告问卷的响应。提交表单后,我将解析表单数据并创建一个具有预定义结构的数组/对象

我正在考虑创建一个具有整个对象层次结构的类,其值设置为空字符串“”。我要存储的响应如下所示:

{
    "_basic": {
        "_response_type": "text",
        "_response": {
            "name": "eNeMetchH",
            "gender": "male",
            "age": 20,
            "contact": "9876543210",
            "email": "email@email.com",
            "dob": "1997-03-20",
            "package": {
                "id": "1001",
                "name": "Alpha Monthly",
                "start_date": "2017-01-01",
                "end_date": "2017-01-31"
            }
        }
    },
    "_parq": {
        "footnotes": "If yes to one or more questions, please talk to your doctor before you start becoming more physically active or before you have a fitness appraisal. Tell your doctor about the PAR-Q and which questions you have answered yes.",
        "response_type": "radio",
        "response": [
            {
                "question": "Has your doctor ever said you have a heart condition and you should only do physical activity that is described by doctor?",
                "response": "NO"
            },
            {
                "question": "Do you feel pain in your chest when you do physical activity?",
                "response": "NO"
            },
            {
                "question": "In the past month, have you had chest pain when you were not doing physical activity?",
                "response": "NO"
            },
            {
                "question": "Do you lose your balance because of dizziness or do you ever lose consciousness?",
                "response": "NO"
            },
            {
                "question": "Do you have a bone or joint problem that could not be made worse by a change in your physical activity?",
                "response": "NO"
            },
            {
                "question": "Is your doctor currently prescribing drugs for your BP & Heart conditions?",
                "response": "NO"
            },
            {
                "question": "Do you know any other reason why you should not do physical activity?",
                "response": "NO"
            }
        ]
    },
    "_conditioning_level": "_intermediate_1_3",
    "_occupation": "_professional",
    "_primary_goal": "_weight_gain",
    "_secondary_goal": "_muscular_strength",
    "_workout_intensity": "_mild",
    "_availability": {
        "_days_per_week": "6",
        "_min_per_day": "90"
    },
    "_sleep": "_insomnia",
    "_physical_activity": "_sedentary",
    "_stress_level": "_mild",
    "_medical_history": [
        "_knee_pain",
        "_hypertension",
        "_high_cholesterol"
    ],
    "_alcohol": "_alcoholic",
    "_smoking": "YES",
    "_tobacco": "NO",
    "_shoe_analysis": {
        "_toe_box": "_normal",
        "_torsion": "_normal",
        "_heel_support": "_abnormal",
        "_arch": "_normal"
    },
    "_postural_analysis": {
        "_head_neck": "Blah blah",
        "_shoulder": "Blah blah",
        "_thoracic_spine": "Blah blah",
        "_lumbar_spine": "Blah blah",
        "_knee": "Blah blah",
        "_feet": "Blah blah"
    },
    "_anthropometry": {
        "_bp": "160/90 mm/Hg",
        "_resting_heart_rate": "90 bpm",
        "_weight": "82",
        "_height": "183",
        "_bmi": "20"
    },
    "_functional_movement": {
        "_ssn": "Blah blah",
        "_height": "183",
        "_weight": "82",
        "_primary_score": "Dono",
        "_primary_position": "Dono",
        "_hand_leg_dominance": "Dono",
        "_previous_test_score": "Dono",
        "tests": {
            "_deep_squat": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_hurdle_step": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_inline_lunge": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_shoulder_mobility": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_impingement_clearing_test": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_active_straight_leg_raise": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_trunk_push_up": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_press_up_test": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_rotary_stability": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_posterior_rocking_test": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_total": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ]
        }
    },
    "_fitness_test": {
        "_aerobic_endurance": {
            "_distance_covered": "2 km",
            "_pre_heart_rate": "72",
            "_post_heart_rate": "95",
            "_vo2_max": "90",
            "_mets": "30"
        },
        "_muscular_strength": {
            "_upper_body": "6",
            "_lower_body": "7"
        },
        "_muscular_endurance": {
            "_ab_crunches": "6",
            "_free_squats": "7"
        },
        "_flexibility": {
            "_sit_reach": "5",
            "_cobra_stretch": "6"
        },
        "_proprioception": {
            "_lt_side": "10",
            "_rt_side": "10"
        },
        "_core_strength": {
            "_iron_man": "55"
        }
    },
    "_assessment_summary": {
        "_age": "20",
        "_sex": "_male",
        "_conditioning_level": "_intermediate_1_3",
        "_goal": "_weight_gain",
        "_occupation": "_professional",
        "_stress_level": "_mild",
        "_health_problem": "None",
        "_test_result": "Need fat loss and Increment in lung capacity",
        "_preferences": "None whatsoever"
    },
    "_remarks": "Blah blah blah"
}
我需要创建一个模板与上述结构完整和空值。不允许添加更多密钥或删除密钥


最好的方法是什么?

如果您将变量保持为受保护的,那么它只能由成员函数修改,不能直接访问。_u_set函数将阻止添加新变量

创建一个类

class Foo{
// if you keep the variable as protected, then it can be modified only
// by member functions so noone will be able to remove the variable. 
protected $v1;

// this set function will prevent addition of new variable.
public function __set($name, $value) {
    throw new Exception("Cannot add new property \$$name to instance of " .   __CLASS__);
}

}
例如,对于“占领”等属性,必须将其定义为

protected $occupation = "professional";

您应该利用PHP中的面向对象编程来创建具有所需属性的类,这是确保不添加或删除字段的唯一方法removed@jiboulex你会怎么做?请您将其显示为一个更简单的结构。我如何定义$v1的结构?@eNeMetcH,我已经编辑了我的ans以回答您的评论(如果这是您要求的)