Check for JSON value with PHP

Asked

Viewed 1,035 times

1

I have a variable that returns a certain number that is correct.

Example:

$cc = "52532005536430673";

My question is: how do I verify that this number is present in a JSON? As below:

{
    "testadas": {
        "52532005536430673|04|2023|869": {
            "cc": "52532005536430673",
            "dados": "\ud83c\udde7\ud83c\uddf7",
            "status": "Reprovada",
            "data_hora": "02-09-2018 20:45:13",
            "retestada": false
        }
    }
}

In this case it is present in the "CC" and always want to check if the number is present in the "CC".

2 answers

0


Basically, you can convert this json to an object or array, and then go through all the items that are in the "tested" by making a check if the cc field is not empty.

In the example below, I converted json to array, using json_decode, and passed as first parameter the json and second value 1, which signals that I want to convert to array and not object.

$json = '{
"testadas": {
    "52532005536430673|04|2023|869": {
        "cc": "52532005536430673",
        "dados": "\ud83c\udde7\ud83c\uddf7",
        "status": "Reprovada",
        "data_hora": "02-09-2018 20:45:13",
        "retestada": false
    }
  }
}';

$resultado_json = json_decode($json,1);
$cartao = "52532005536430673";
foreach($resultado_json["testadas"] as $item){
  if($item["cc"] == $cartao){
    print "Numero presente no campo cc";
  }
}
  • I think this would not work properly, because I have several cc fields in json, and I need to check the variable!

  • Could show an example of a json that has more than one cc?

  • https://ghostbin.com/paste/bfqpj !

  • So, the way I introduced him, he’s going through every item of json. First he will check whether in the key "52532005536430673|04|2023|869" there is the cc field, and then whether in the key "52532005536400673|04|2023|869" there is this field in it. There is no problem that there is more than one "cc" field, since it will only validate the context of each key in each json item.

  • Okay, but you end up with the same "error" that I told Gabriel, the foreach returns nothing!

  • The first json_decode parameter must be a string and not a file. To get the value that is in the file you want can be done this way: json_decode(file_get_contents($file2),1);

  • Okay, I was able to solve it, but now for me to echo the info, like the data for example, would this be it?: echo $item["$card"]->data;

  • It would look like this: echo $item['data']; it will not be necessary to pass the card ID to print the other columns inside the foreach.

  • 1

    It worked, thanks partner! !

Show 4 more comments

0

Here is the code working, it takes the cc of the JSON and then compares it to the value of the variable. To get from an external file, it is necessary to use file_get_contents() to get the JSON link:

PHP file:

<?php
$cc = "52532005536430673";
$file = "ccs.json";

$json_file = file_get_contents($file);
$json_result = json_decode($json_file);

foreach($json_result->testadas as $item){
    if($item->cc == $cc){
        print "Esse número está presente em cc";
    }
    else{
        print "Esse número não está presente em cc";
    }
}
?>

ccs.json

{
    "testadas": {
        "52532005536430673|04|2023|869": {
            "cc": "52532005536430673",
            "dados": "\ud83c\udde7\ud83c\uddf7",
            "status": "Reprovada",
            "data_hora": "02-09-2018 20:45:13",
            "retestada": false
        }
    }
}
  • https://ideone.com/Mnu8Ek It ended up being this way, but when I test it does not return anything, and I echo in the $card and everything, and it is all right! Only it does not return anything in foreach

  • I edited the code, according to what you put in ideone, taking the JSON of a file. Now test and see how it works.

  • Now it has returned, but returned the 2: This number is not present in ccThis number is present in cc kk

  • strange, should not enter if and Else at the same time and I just ran the code here and it’s working

  • https://ideone.com/CVu59f This is the final code, but is returning the 2

  • Okay, I was able to solve it, but now for me to echo the info, like the data for example, would this be it?: echo $item["$card"]->data;

  • echo "<b>Data:</b> ". $item->data;

  • Returned nothing!

  • Are you doing it right? Because, according to the code you put on Idone, it is printing everything that is written, not the result.

  • I was able to do according to the code of Andre echo $item['data'];

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.