dtny30176
2017-09-18 13:14
浏览 183

检查json foreach循环中是否存在字符串

I am trying to check if a value exists inside an foreach loop from a decoded json response and compare it to my own string. I need to set $response_array['status'] to "Allowed" if $domain_to_check value exists inside the $key_info['registered_domain'] array. I tried to use in_array php function to check if value exists, however i had no success and i keep getting back "Not Allowed - Domain not listed" response even when the value is inside the array. I think that the problem is with my foreach loop but for the sake of me i can't figure whats wrong.

$domain_to_check = 'domain-name.com';
$data = json_decode($returnCheckValue,true);
$key_response = $data['result'];
if ($key_response == 'success'){
    foreach ($data['registered_domains'] as $key_domain_info) {
        $key_listed_domain = $key_domain_info['registered_domain'];
        if ($key_response == 'success' && in_array($domain_to_check, $key_listed_domain)) {
            $response_array['status'] = 'Allowed';
        }
        else {
            $response_array['status'] = 'Not Allowed - Domain not listed';
        }
    }
}
else {
    $response_array['status'] = 'Not Allowed - Wrong Key';
}
echo json_encode($response_array);

Here is how my var_dump(); of the $data looks like

array(9) { ["result"]=> string(7) "success" ["max_allowed_domains"]=> string(1) "3" ["registered_domains"]=> array(2) { [0]=> array(5) { ["id"]=> string(2) "60" ["lic_key_id"]=> string(2) "51" ["lic_key"]=> string(13) "93248cqkdj21as" ["registered_domain"]=> string(19) "domain-name-2.com" ["item_reference"]=> string(1) "1" } [1]=> array(5) { ["id"]=> string(2) "58" ["lic_key_id"]=> string(2) "51" ["lic_key"]=> string(13) "93248cqkdj21as" ["registered_domain"]=> string(14) "domain-name.com" ["item_reference"]=> string(3) "443" } } }

图片转代码服务由CSDN问答提供 功能建议

我试图检查来自解码的json响应的foreach循环中是否存在值并将其与我自己的比较 串。 如果$ key_info ['registered_domain']数组中存在$ domain_to_check值,我需要将$ response_array ['status']设置为“Allowed”。 我试图使用in_array php函数来检查值是否存在,但是我没有成功,并且即使值在数组内部,我也会继续回复“Not Allowed - Domain not listed”响应。 我认为问题在于我的foreach循环但是为了我,我无法弄清楚什么是错的。

  $ domain_to_check ='domain-name.com'; \  n $ data = json_decode($ returnCheckValue,true); 
 $ key_response = $ data ['result']; 
if($ key_response =='success'){
 foreach($ data ['registered_domains'] as $  key_domain_info){
 $ key_listed_domain = $ key_domain_info ['registered_domain']; 
 if($ key_response =='success'&& in_array($ domain_to_check,$ key_listed_domain)){
 $ response_array ['status']  ='允许'; 
} 
其他{
 $ response_array ['status'] ='不允许 - 未列出域名'; 
} 
} 
} 
else {
 $ response_array ['status  '] ='不允许 - 错误的密钥'; 
} 
echo json_encode($ response_array); 
   
 
 

以下是我的var_dump(); $ data的数据类似于

  array(9){[“result”] =>  string(7)“success”[“max_allowed_domains”] =>  string(1)“3”[“registered_domains”] =>  array(2){[0] =>  array(5){[“id”] =>  string(2)“60”[“lic_key_id”] =>  string(2)“51”[“lic_key”] =>  string(13)“93248cqkdj21as”[“registered_domain”] =>  string(19)“domain-name-2.com”[“item_reference”] =>  string(1)“1”} [1] =>  array(5){[“id”] =>  string(2)“58”[“lic_key_id”] =>  string(2)“51”[“lic_key”] =>  string(13)“93248cqkdj21as”[“registered_domain”] =>  string(14)“domain-name.com”[“item_reference”] =>  string(3)“443”}}} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlangwen9597 2017-09-18 14:22
    已采纳

    Relate below code with your code. This code is working.

        $domain_to_check = "domain-name.com";
    
        $test = array("registered_domains" => array("registered_domain" => "domain-name-2.com"), array("registered_domain" => "domain-name.com"));
    
        foreach($test as $val) {
            if($val['registered_domain'] == $domain_to_check) {
                $result = 'success';
                break;
    
            } else {
                $result = 'failure';
            }
        }
    
        echo $result;
    
    已采纳该答案
    打赏 评论
  • donglu4633 2017-09-18 13:24

    Use php strpos

    $domainStringFound = strpos($key_listed_domain, $domain_to_check);
    
    if ($key_response == 'success' && $domainStringFound !== false) {
      $response_array['status'] = 'Allowed';
    }
    
    打赏 评论

相关推荐 更多相似问题