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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。