Check if pattern is followed, JSON and PHP

Asked

Viewed 1,135 times

0

There is a small request pattern with multiple parameters, but I’ll simplify it this way:

$parametrosdBase = array('nome' => false, 'codigo' => 83474, etc..

I get a string in JSON format that theoretically should CONTAIN these parameters, ie it has to have all those parameters that I showed in the array above.

Yes I could simply check if each parameter exists manually but as it is on a large scale I would like a function that does this automatically.

Obs: the parameter may even be vazio, false, 0 the important thing is he exist.

  • Just want to check if the received string is a JSON?

  • @mauriciocaserta Hi, no, it’s to check if the parameters were followed.

  • These parameters are on the root level of json?

  • You want to compare the keys to $parametrosdBase with json keys or you also want to compare $parametrosdBase values with values within json?

  • @Guilhermenascimento Hi, maybe I used the wrong words, not to compare values. it goes like this: I have that base array, and I want to check in a JSON format string that all parameters of the base array are also present in the encoded string, regardless of whether they are empty, false, 0 etc..

  • I think she wants to check if there are 'name', 'code' keys inside the array?

  • Elaine I did not state that it was comparing values, I asked if they were values and keys or just keys, I will assume that are the keys you need, I will formulate an answer.

Show 2 more comments

2 answers

2


If with parameters, you mean "keys" then you can do using array_keys that will take the key name, code, etc from the variable $parametrosdBase and use the array_key_exists to check if the key exists (since you said the values can be empty, false I suppose they can be null also).

Example:

<?php
$parametrosdBase = array('nome' => false, 'codigo' => 83474);

$json = json_decode('{"nome": "João", "codigo": false, "foo": "hello" }', true);

$chaves = array_keys($parametrosdBase);

$error = null;

foreach ($chaves as $value) {
    if (false === array_key_exists($json[$value])) {
        $error = 'Parametro "' . $value . '" não encontrado';
        break;
    }
}

if ($error) {
    echo $error;
} else {
    echo 'Todos parametros da base encontrados!';
}

isset vs array_key_exists

I switched the isset for array_key_exists, because if you have any value with null, then he will give false chance use isset, even if the key exists.

<?php
$search_array = array('first' => null, 'second' => 4);

// Retorna false
isset($search_array['first']);

// Retorna true
array_key_exists('first', $search_array);

What are keys in an array

Note that these parameters to which they refer are usually called "keys" or Keys (see), see:

PHP:

array('nome'                  => 'João');
        ^---Isto é uma chave       ^-----Isto é um valor de uma chave

Json:

{'nome':                     'João'};
    ^---Isto é uma chave       ^-----Isto é um valor de uma chave
  • Now, these little things are called keys? Didn’t know, learned something and solved problems, Thank Uuuu

  • 1

    @Elaine updated her answer.

0

Try the following code:

$entrada = json_decode( $json, true );
$params = array( ... );
$intersecao = array_intersect_assoc( $entrada, $params );
$sucesso = count( $intersecao ) == count( $params );

Browser other questions tagged

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