PHP多维数组 - 我是否需要此密钥?

时间:2014-12-08 12:58:04

标签: php arrays multidimensional-array associative-array

我在php中创建了我的第一个关联多维数组,我想知道一个关键 - 是否使用它。我正在创建一个用户输入食物名称的表单。当他们提交表单时,php处理程序将搜索我的多维数组,查看是否匹配,如果是,则显示与该食物相关的数据。

以表格形式,这很简单:

NAME / VALUE / ADD' L INFO / NUTRITIONAL INFO 苹果/是/适合你/卡路里:57                                    份量:1杯(等 - 这里有很多值)

所以我认为我有两个选择:

//first possible choice
$yesorno = array( 
               array( Name => "apples", 
                      Value => "yes",
                      Addinfo => 15 
                      nutrinfo => array (
                                                           "Serving Size" => "1 cup sliced",
                                                           "Calories" => 57,
                                                           "Carbohydrates" => "15 g",
                                                           "Total Fat" => "0.2 g",
                                                           "Saturated Fat" =>  "0 g",
                                                           "Fiber" =>  "2.6 g",
                                                           "Protein" => "0.3 g" ,
                                                           "Vitamin A" => "59 IU",
                                                           "Vitamin C" => "5 mg", 
                                                           "Vitamin B6" => 0,
                                                           "Vitamin B12" => 0,
                                                           "Vitamin D" => 0,
                                                           "Calcium" => "7 mg",
                                                           "Magnesium" => "5 mg",
                                                           "Iron" => "0.1 mg",
                                                           "Potassium" => "0.1 mg",
                                                           "Sodium" => "1 mg"
                                                          ),
                    ),
               array( Name => "bananas", 
                      Value => "yes",
                      Addinfo => 25,
                    ),
               array( Name => "cauliflower", 
                      Value => "yes",
                      Addinfo => 7 
                    )
             );

或者这个:

$yesorno = array ( 
                  "apples"  => array (  
                                       "name" => "apples",  
                                       "value" => "Yes",  
                                       "addinfo" => "", 
                                       "nutrinfo" => array (
                                                           "Serving Size" => "1 cup sliced",
                                                           "Calories" => 57,
                                                           "Carbohydrates" => "15 g",
                                                           "Total Fat" => "0.2 g",
                                                           "Saturated Fat" =>  "0 g",
                                                           "Fiber" =>  "2.6 g",
                                                           "Protein" => "0.3 g" ,
                                                           "Vitamin A" => "59 IU",
                                                           "Vitamin C" => "5 mg", 
                                                           "Vitamin B6" => 0,
                                                           "Vitamin B12" => 0,
                                                           "Vitamin D" => 0,
                                                           "Calcium" => "7 mg",
                                                           "Magnesium" => "5 mg",
                                                           "Iron" => "0.1 mg",
                                                           "Potassium" => "0.1 mg",
                                                           "Sodium" => "1 mg"
                                                          ),
                                       ), 
                  "bananas"  => array ( 
                                       "name" => "bananas",  
                                       "value" => "yes",  
                                       "addinfo" => "",
                                       "nutrinfo" => array (
                                                           "Serving Size" => "1 cup sliced",
                                                           "Calories" => 57,
                                                           "Carbohydrates" => "15 g",
                                                           "Total Fat" => "0.2 g",
                                                           "Saturated Fat" =>  "0 g",
                                                           "Fiber" =>  "2.6 g",
                                                           "Protein" => "0.3 g" ,
                                                           "Vitamin A" => "59 IU",
                                                           "Vitamin C" => "5 mg", 
                                                           "Vitamin B6" => 0,
                                                           "Vitamin B12" => 0,
                                                           "Vitamin D" => 0,
                                                           "Calcium" => "7 mg",
                                                           "Magnesium" => "5 mg",
                                                           "Iron" => "0.1 mg",
                                                           "Potassium" => "0.1 mg",
                                                           "Sodium" => "1 mg"
                                                          ),
                                       ), 

                  ); 

所以我想知道选项1和选项2之间的区别 - 区别在于选项2有第二个数组的键,而第一个数组没有。我将搜索名称,然后检索名称,值,附加信息和营养信息(包含许多值)。

使用选项2中的密钥或不使用选项1中的密钥更好吗?让#34;苹果" /"香蕉"等两次似乎是多余的。如果我使用密钥,当我通过阵列搜索" apples"时会出现问题。或者该人在表格上输入的任何食物名称,因为同一名称被列出两次?

另外,你真的需要在密钥周围加上引号(例如,"名称")还是可选的?

1 个答案:

答案 0 :(得分:2)

我建议使用后面的数组声明形式,因为没有混淆或冲突来选择具有该关联键的元素,如果你想在这个例子中创建使用key来识别苹果的任何数组,你可以创建一个数组如下,所以它会解决你的冲突。

$yesorno = array ( 'apples'=>array(
                                 'applename'=>array(
                                       "value" => "Yes",  
                                       "addinfo" => "", 
                                       "nutrinfo" => array (
                                                           "Serving Size" => "1 cup sliced",
                                                           "Calories" => 57,
                                                           "Carbohydrates" => "15 g",
                                                           "Total Fat" => "0.2 g",
                                                           "Saturated Fat" =>  "0 g",
                                                           "Fiber" =>  "2.6 g",
                                                           "Protein" => "0.3 g" ,
                                                           "Vitamin A" => "59 IU",
                                                           "Vitamin C" => "5 mg", 
                                                           "Vitamin B6" => 0,
                                                           "Vitamin B12" => 0,
                                                           "Vitamin D" => 0,
                                                           "Calcium" => "7 mg",
                                                           "Magnesium" => "5 mg",
                                                           "Iron" => "0.1 mg",
                                                           "Potassium" => "0.1 mg",
                                                           "Sodium" => "1 mg"
                                                          ),
                                                    ),
                                 'applename2'=>array(
                                       "value" => "Yes",  
                                       "addinfo" => "", 
                                       "nutrinfo" => array (
                                                           "Serving Size" => "1 cup sliced",
                                                           "Calories" => 57,
                                                           "Carbohydrates" => "15 g",
                                                           "Total Fat" => "0.2 g",
                                                           "Saturated Fat" =>  "0 g",
                                                           "Fiber" =>  "2.6 g",
                                                           "Protein" => "0.3 g" ,
                                                           "Vitamin A" => "59 IU",
                                                           "Vitamin C" => "5 mg", 
                                                           "Vitamin B6" => 0,
                                                           "Vitamin B12" => 0,
                                                           "Vitamin D" => 0,
                                                           "Calcium" => "7 mg",
                                                           "Magnesium" => "5 mg",
                                                           "Iron" => "0.1 mg",
                                                           "Potassium" => "0.1 mg",
                                                           "Sodium" => "1 mg"
                                                          ),
                                                     ),
                                  )
                   ); 

如果我理解你的问题,那么我希望这会给你解决方案。并且密钥的引号是必要的,因为如果你分配一个值,解析器会将它视为变量并产生错误,变量是找不到错误。谢谢。