【问题标题】:Function is not returning true or false, if it is correct如果正确,函数不会返回真或假
【发布时间】:2014-02-26 11:37:10
【问题描述】:

我正在编写一个函数来检查嵌套键是否存在于 JSON 中,但是当代码正确时我卡在了原地,那么它必须返回 true 或 false,但事实并非如此。它返回空值

php函数是

function checkNestedKeysExists($JSONRequest,$keyCheckArray){
$currentKey = current($keyCheckArray);
$JSONRequest = array_change_key_case($JSONRequest, CASE_LOWER); 

    if(array_key_exists($currentKey,$JSONRequest)){
        if($currentKey==end($keyCheckArray)){
            return true;            
        }    
        else { 
            array_shift($keyCheckArray);  
            $this->checkNestedKeysExists($JSONRequest[$currentKey],$keyCheckArray);                
            //echo "F";
        }    
    }
    else{
        return false;
    }
}

给定数组是

$keyCheckArray = array('data','device_info','deviceid');

$JSONRequest 是

{
"timestamp": "2014-01-01 11:11:11",
"data": {
    "requestid": "bcpcvssi1",
    "device_info": {
        "os": "Android",
        "deviceId": "123123",
        "userProfile": {
            "email": [
                "abc@gmail.com"
            ],
            "gender": "Male",
            "age": "19",
            "interest": [
                "Apple",
                "Banana"
            ]
        }
    }
}
}

【问题讨论】:

  • 需要返回递归函数的输出。 return $this->checkNestedKeysExists($JSONRequest[$currentKey],$keyCheckArray);.
  • 感谢@h2ooooooo 它有效!。
  • 这是我在回答中给你的第一个建议。
  • @LoekBergman OP 很可能没有意识到他的缺陷,因此“为函数提供所有可能的方法来结束函数一个有效的返回值。”可能没有用对正在进入该语言的人。如果它是如此明显,OP可能会自己完成。 :-)
  • @h2ooooooo:有道理,我以前听过,我有时说话太抽象了。好吧,我希望OP仍然会接受他的建议。如果我从编程一开始就这样做,那么我需要解决的问题就会更少。

标签: php json


【解决方案1】:

修改进行递归调用的代码行,如下所示

return $this->checkNestedKeysExists($JSONRequest[$currentKey],$keyCheckArray); 

所以它会返回调用的结果

【讨论】:

    【解决方案2】:

    中传递 $JSONRequest
    json_decode($JSONRequest, true);
    

    【讨论】:

    • 他正在使用数组函数。也许你不得不提到创建数组的第二个参数;)
    【解决方案3】:

    编辑:对不起,我第一次弄错了。 如果要移动元素,请使用 array[0] 而不是 current(),这可能会造成问题。当然,请执行var_dump() 来检查值。

    【讨论】:

      【解决方案4】:

      $currentkey = 'data' 和 end($keyCheckArray) = 'deviceid'。这永远不会返回 true,因此您没有指定返回值,它将返回 null。

      两个建议:

      1. 为函数提供所有可能的结束函数的有效返回值。

      2. 为每个固定结果创建一个变量,例如 end($keyCheckArray)。

      如果已测试您的功能(并出于测试目的对其进行了编辑):

      function checkNestedKeysExists($JSONRequest,$keyCheckArray){
        $currentKey = current($keyCheckArray);
        $JSONRequest = array_change_key_case($JSONRequest, CASE_LOWER); 
        $endValue = end($keyCheckArray);
      
      if(array_key_exists($currentKey,$JSONRequest)){
          print 'currentKey = '.$currentKey.", end = ".$endValue."<br>\n";
          if($currentKey== $endValue){
              return 'correct';            
          }else { 
              array_shift($keyCheckArray);  
              $p = checkNestedKeysExists($JSONRequest[$currentKey],$keyCheckArray);
              print "p = ".$p."<br>\n";
              //echo "F";
              return $currentKey;
          }    
      }
      else{
          return false;
      }
      }
      

      输出是这样的: 正确

      设备信息

      数据

      我建议您将函数更改为 while 循环。找到请求的结果后,返回 true。

      【讨论】:

      • 干运行代码,由于递归,它到达 $currentkey = 'deviceid' 和 end($keyCheckArray) = 'deviceid' 的点。并且它有一个有效的返回函数,end($keyCheckArray) 总是固定的
      • 为固定结果创建变量是利用享元设计模式并且对内存使用友好。
      • 当我运行您的代码时,我收到消息说 $this-> 等没有对象引用是不可能的。此函数是对象内的方法。你的 $JSONRequest 是一个数组,因此在你让它进入函数之前已经做了一些工作。因此,它与您提供给我们的 $JSONRequest 不同。这会让研究变得太困难。
      猜你喜欢
      • 2015-09-29
      • 2021-11-05
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2015-10-05
      • 2016-08-17
      • 2019-10-15
      • 2014-09-07
      相关资源
      最近更新 更多