如何将json文件中的值与PHP匹配

如何将json文件中的值与PHP匹配

问题描述:

I am saving data in JSON file for retrieving after redirect. If I check if the value matches in the json array, I always get false and something must be wrong, but I cannot figure it out. How can I the object with the specific SSL_SESSION_ID

Here's my json

[
    {
        "SSL_CLIENT_S_DN_CN": "John,Doe,12345678912",
        "SSL_CLIENT_S_DN_G": "JOHN",
        "SSL_CLIENT_S_DN_S": "DOE",
        "SSL_CLIENT_VERIFY": "SUCCESS",
        "SSL_SESSION_ID": "365cb4834f9d7b3d53a3c8b2eba55a49d5cac0112994fff95c690b9079d810af"
    },
    {
        "SSL_CLIENT_S_DN_CN": "John,Doe,12345678912",
        "SSL_CLIENT_S_DN_G": "JOHN",
        "SSL_CLIENT_S_DN_S": "DOE",
        "SSL_CLIENT_VERIFY": "SUCCESS",
        "SSL_SESSION_ID": "e7bd2b6cd3db89e2d6fad5e810652f2ae087965e64b565ec8933b1a67120b0ac"
    }
]

Here's my PHP script, which always returns It doesn't match

            $sslData     = Storage::disk('private')->get('ssl_login.json');
            $decodedData = json_decode($sslData, true);
            foreach ($decodedData as $key => $items) {

                if ($items['SSL_SESSION_ID'] === $SSL_SESSION_ID) {
                    dd("It matches");


                } else {
                    dd("It doesn't match");

                }
            }

我将数据保存在JSON文件中,以便在重定向后检索。 如果我检查json数组中的值是否匹配,我总是得到假,一定是错的,但我无法弄明白。 如何使用特定的 SSL_SESSION_ID code> p>

这里的对象这是我的json p>

  [
 {
  “SSL_CLIENT_S_DN_CN”:“John,Doe,12345678912”,
“SSL_CLIENT_S_DN_G”:“JOHN”,
“SSL_CLIENT_S_DN_S”:“DOE”,
“SSL_CLIENT_VERIFY”:“SUCCESS”,
“SSL_SESSION_ID”:“365cb4834f9d7b3d53a3c8b2eba55a49d5cac0112994fff95c690b9079d810af  “
},
 {
”“SSL_CLIENT_S_DN_CN”:“John,Doe,12345678912”,
“SSL_CLIENT_S_DN_G”:“JOHN”,
“SSL_CLIENT_S_DN_S”:“DOE”,
“SSL_CLIENT_VERIFY”:“成功 “,
”SSL_SESSION_ID“:”e7bd2b6cd3db89e2d6fad5e810652f2ae087965e64b565ec8933b1a67120b0ac“
} 
] 
  code>  pre> 
 
 

这是我的PHP脚本,它始终返回它不匹配 code> p>

  $ sslData = Storage :: disk('private') - > get('ssl_login.json'); 
 $ decodingData = json_decode($  sslData,true); 
 foreach($ decodingData as $ key => $ items){
 
  if($ items ['SSL_SESSION_ID'] === $ SSL_SESSION_ID){
 dd(“匹配”); 
 
 
} else {
 dd(“它不匹配”); 
  
} 
} 
  code>  pre> 
  div>

Your script will always end after the first iteration, since you're using dd() regardless if it was a match or not and you will always get the "no-match"-message if the first iteration isn't a match.

You should iterate through all the items and then see if there was a match or not:

$match = null;
foreach ($decodedData as $key => $items) {
    if ($items['SSL_SESSION_ID'] === $SSL_SESSION_ID) {
        $match = $items;
        // We found a match so let's break the loop
        break;
    }
}

if (!$match) {
    dd('No match found');
}

dd('Match found');

That will also save the matching item in the $match variable if you need to use the data somehow.