dongyan3562 2018-05-28 05:20
浏览 175
已采纳

如何将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");

                }
            }
  • 写回答

1条回答 默认 最新

  • donglu5000 2018-05-28 05:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?