你知道如何测试,根据我们传入字符串的内容,索引是否存在于数组中?
假设以下代码:
<?php
$myArray = [
"elemOne" => "valueElemOne",
"elemTwo" => [
"elemTwoOne" => "valueElemTwoOne",
"elemTwoTwo" => [
"elemThreeOne" => "valueElemThreeOne",
"elemThreeTwo" => "valueElemThreeTwo",
],
],
];
现在,我有一个字符串$myString = "elemTwo/elemTwoTwo/elemThreeThree"
。我想用$myString
做什么在某种程度上是格式化的,所以我可以检查
<?php
if(isset($myArray['elemTwo']['elemTwoTwo']['elemThreeThree'])) {
// maybe do something
return true;
} else {
return false;
}
当然,在我的情况下,这将返回false,因为我的数组中不存在索引"elemThreeThree"
。我尝试拆分字符串,尝试格式化为[elemTwo][elemTwoTwo][elemThreeThree]
然后评估它,但没有真正有效。
你认为一种可能对我有帮助的方法吗?
答案 0 :(得分:1)
简单的解决方案可能是
UILabel
函数的输出将为true或false
答案 1 :(得分:0)
为什么分裂它不起作用? 这有什么问题吗?
<?php
$arrayTest = array(
"arrayOne" => "string",
"arrayTwo" => array(
"arrayThree" => "string",
"arrayFour" => array("winner" => "me")
)
);
var_dump(expanded_key_exists($arrayTest, "arrayOne"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayThree"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayThree/arrayTwenty"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayFour"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayFour/winner"));
if(expanded_key_exists($arrayTest, "arrayTwo/arrayFour/winner")){
echo "The winner is: " . expanded_array_key($arrayTest, "arrayTwo/arrayFour/winner");
}
function expanded_key_exists($array, $key){
$arrayKeys = explode("/", $key);
foreach($arrayKeys as $someKey){
if(is_array($array) && array_key_exists($someKey, $array)){
$array = $array[$someKey];
}else{
return false;
}
}
return true;
}
function expanded_array_key($array, $key){
$arrayKeys = explode("/", $key);
foreach($arrayKeys as $someKey){
if(is_array($array) && array_key_exists($someKey, $array)){
$array = $array[$someKey];
}else{
return null;
}
}
return $array;
}
?>