How to filter and sort rewritten JSON elements with PHP?

Asked

Viewed 83 times

-3

The script below fetches the JSON file and restructures it into a new template.

<?php

$json_strcont = file_get_contents("http://app.tce.ma.gov.br:8889/remessas?enteId=2102309"); //Puxa todas as remessas
$objcont = json_decode($json_strcont); //Decoficando JSON

$output = array();
foreach ($objcont->content as $content) { //Transforma o JSON decodificado em Array
    $item = array(
        "tiporemessa" => $content->tipoRemessa->nome,
        "periodo" => $content->periodo->nome,
        "exercicio" => $content->historicoRemessa->exercicio,
        "dataenvio" => $content->dataEnvio,
        "arquivo" => "http://app.tce.ma.gov.br:8889/remessas/download/" . $content->arquivo
    );
    $output[] = $item;
}
echo json_encode($output, JSON_PRETTY_PRINT); //Imprime o array recodificando-o em JSON

?>

Before | Return - New JSON

How to order the new print JSON by date ( $content->dataEnvio ) from the latest to the oldest and delete / ignore those that have no date, ie, dataEnvio = null?

And how to filter all the elements that contain the object ( tipoRemessa->nome ) with the value ( RGF - LEGISLATIVO MUNICIPAL ) and delete / ignore in new JSON?

I found no question about filtering and ordering the output of a rewritten JSON.

  • 1

    And when dataEnvio for null? :)

  • @Luizfelipe then its respective element is ignored / deleted from the list as those who are null do not serve. =)

  • 1

    I think it’s worth researching about the function usort PHP. Also, regarding the empty dates, just don’t do the push ($output[] = $item) to the new array if the date is null (and any other criteria you wish).

  • @Luizfelipe can turn your comment into a reply?

1 answer

2


I found no question about filtering and ordering the output of a rewritten JSON.

There is no such thing as filtering JSON, you write already filtering the object, it is you who is controlling the object before it is JSON, transforming into JSON is the last step, what matters is to understand what you want and make use of the basic language, learn the basics, I’ll put parts to understand better:

delete / ignore those that have no date, ie dateEnvio = null?

But that’s the minimum basic principle of programming, use if (), plain as that:

foreach ($objcont->content as $content) { //Transforma o JSON decodificado em Array
    if ($content->dataEnvio) {
        $item = array(
            "tiporemessa" => $content->tipoRemessa->nome,
            "periodo" => $content->periodo->nome,
            "exercicio" => $content->historicoRemessa->exercicio,
            "dataenvio" => $content->dataEnvio,
            "arquivo" => "http://app.tce.ma.gov.br:8889/remessas/download/" . $content->arquivo
        );
        $output[] = $item;
    }
}

And how to filter all elements that contain the object ( typeRemessa->name ) with the value

If it is you who controls the JSON that will still be generated is if, is the basic, is the minimum, if you expect an exact value, then it would be within the foreach:

if ($tipoRemessa->nome == 'RGF - LEGISLATIVO MUNICIPAL') {
   ...
}

Since there are two needs, you have to use the && to consider both conditions:

foreach ($objcont->content as $content) { //Transforma o JSON decodificado em Array
    if ($content->dataEnvio && $tipoRemessa->nome == 'RGF - LEGISLATIVO MUNICIPAL') {
        $item = array(
            "tiporemessa" => $content->tipoRemessa->nome,
            "periodo" => $content->periodo->nome,
            "exercicio" => $content->historicoRemessa->exercicio,
            "dataenvio" => $content->dataEnvio,
            "arquivo" => "http://app.tce.ma.gov.br:8889/remessas/download/" . $content->arquivo
        );
        $output[] = $item;
    }
}

Finally, to sort you have the documentation of PHP itself for arrays (because as I said you treat the object, make into JSON is after that):

I I cannot answer about sort, because at no point in the question did you say what that sort should be like, unless you’re confusing sorting with filtering, anyway, you don’t sort or filter JSON, you do it with the object (the array), because you control it, and for this you can use this function (which is in the documentation):

I do not want to criticize or give you personal guesses, but I do not understand, this is the basic programming and you are a member of the site for 5 years, how can you skip the basic steps? All of this is learned in the beginning, focus on learning the basics of the new beginning, because this will help you avoid problems and even exaggerated codes. You may think it will take time, but there’s no way forward without the basics.

  • William, I tested here but I had to put $content-> before the tipoRemessa->nome == 'RGF - LEGISLATIVO MUNICIPAL' in the if. I was able to sort the way I wanted it already. I just couldn’t put another condition. I tried to put && ahead with another condition, but it didn’t work. About being a member 5 years ago, I spent some time away, I was more dedicated to frontend, but now I’m studying backend in depth.

  • @Alexandrelopes IF has nothing to do with backend, has if everywhere, logic is independent of "side" in this case, unless your front refers to Adobe XD or Photoshop. I’ve seen questions about PHP for a few years, and it’s always at the same stage, failing at the basics... This IF of yours is all wrong if ($content->dataEnvio && $content->tipoRemessa->tipoRemessaId == '1' && $content->tipoRemessa->tipoRemessaId == '2') {, how do you want to $content->tipoRemessa->tipoRemessaId is equal to 1 and 2 at the same time? This makes sense to you?

  • I tried so: if ($content->dataEnvio && $content->tipoRemessa->tipoRemessaId == '1' || $content->tipoRemessa->tipoRemessaId == '2') { worked, using the operator || but the date no longer worked.

  • Exactly, a few years ago... but I went away for a long time, I returned to studies recently.

  • 1

    @Alexandrelopes does not mix && com ||. So the questions you ask about PHP a long time ago that commits the same flaws of this, even the old ones, that I see in different years, were random?

  • What would be the right way? =)

  • 1

    @Alexandrelopes does not mix && com ||, this is the right way, is to know what you need, learn the basics... I’ve already answered other questions, but it’s not today that I see you sinning in the basics, and today I decided to try to open your mind, but I really think you’re not wanting to learn, you just want something done, I’m not talking out of the blue, but I do not support in a community to teach the program you keep asking Helpdesk.

  • Worse than yes, at the time I asked only in theory, now I’m asking in practice... I admit I rested, I devoted myself to other things... but I’m confident, I’m watching many videos about, I’m studying programming logic, but in php works in other words.

  • I did so: if ($content->dataEnvio && $content->tipoRemessa->tipoRemessaId == '1' or $content->dataEnvio && $content->tipoRemessa->tipoRemessaId == '2') { and it worked. Thank you so much for opening my mind, I managed to unroll here. All right. Thank you.

Show 4 more comments

Browser other questions tagged

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