检查PHP

时间:2016-12-03 19:19:16

标签: php arrays if-statement multidimensional-array key-value

为新手问题道歉,但我对PHP比较陌生,想要做一些相当简单的事情...如果我有一系列的按键&价值观(梨):

$arrayOfPears = array(
                      "Green Anjou" => [ 7,
                                         "plump",
                                         "ripe",
                                         "juicy",
                                         "green"
                                        ],
                      "Bartlett"     => [ 2,
                                          "moderately plump",
                                          "unripe",
                                          "sweet",
                                          "greeny-yellow"
                                         ],
                      "Comice"       =>  [ 3,
                                           "rather plump",
                                           "ripe",
                                           "blinding",
                                           "deep-green"
                                         ],
                      "Plum"          => [ 6,
                                           "rotund",
                                           "over-ripe",
                                           "sweet",
                                           "purple",
                                           "not really a pear, as far as I'm aware"
                                          ]
                      );

我能够访问特定梨的值并将其用于比较操作。例如:

if($arrayofPears['Comice'][2] == "ripe") {
   $nibble = true;
   return $nibble;
}

返回真实的$nibble。但是,我希望能够做的是首先检查数组中是否存在Comise键,之前我尝试输入if语句并查明是否得到了是$nibble

那么......在承诺输入条件之前,我如何检查在这种情况下是否存在'Comice'?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

if(array_key_exists("Comice",$arrayOfPear){
//Do the Loop
}
else{
//Don't enter the loop
}